Android 如何通过 TabLayout 使用 TabItem
How use TabItem through TabLayout in Android
最近 google 在 supportDesign
中添加了 android.support.design.widget.TabItem
正如 documentation 所说:
TabItem is a special 'view' which allows you to declare tab items for
a TabLayout within a layout. This view is not actually added to
TabLayout, it is just a dummy which allows setting of a tab items's
text, icon and custom layout.
但是当我将 TabItems
添加到我的 TabLayout
时:
<android.support.design.widget.TabLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<android.support.design.widget.TabItem
android:text="@string/tab_text"/>
<android.support.design.widget.TabItem
android:icon="@drawable/ic_android"/>
</android.support.design.widget.TabLayout>
没有显示(实际上标签的位置存在但 Icon/Text 不存在)。有谁知道如何使用 TabItem
到 xml?
您应该为 TabItems 设置这些属性
android:layout_width
android:layout_height
干杯
根据 的回答,TabItem 与 tabLayout.setupViewPager
有冲突并且图标消失。要使其工作,您应该实现以下两种方法,并避免使用 setupViewPager
方法:
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
pager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
tabLayout.getTabAt(position).select();
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
最近 google 在 supportDesign
中添加了 android.support.design.widget.TabItem
正如 documentation 所说:
TabItem is a special 'view' which allows you to declare tab items for a TabLayout within a layout. This view is not actually added to TabLayout, it is just a dummy which allows setting of a tab items's text, icon and custom layout.
但是当我将 TabItems
添加到我的 TabLayout
时:
<android.support.design.widget.TabLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<android.support.design.widget.TabItem
android:text="@string/tab_text"/>
<android.support.design.widget.TabItem
android:icon="@drawable/ic_android"/>
</android.support.design.widget.TabLayout>
没有显示(实际上标签的位置存在但 Icon/Text 不存在)。有谁知道如何使用 TabItem
到 xml?
您应该为 TabItems 设置这些属性
android:layout_width
android:layout_height
干杯
根据 tabLayout.setupViewPager
有冲突并且图标消失。要使其工作,您应该实现以下两种方法,并避免使用 setupViewPager
方法:
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
pager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
tabLayout.getTabAt(position).select();
}
@Override
public void onPageScrollStateChanged(int state) {
}
});