Screen shot of Tab where text color remains highlighted 我正在使用android视图寻呼机和标签布局。在xml的标签布局中,我使用此代码
设置了tablayout `<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabBackground="@color/LightGrey"
app:tabIndicatorColor="@color/app_highlight_color"
app:tabIndicatorHeight="3dp"
app:tabTextAppearance="@style/MyCustomTextAppearance"
app:tabSelectedTextColor="@color/app_highlight_color"
app:tabGravity="fill"/>`
问题是当我通过向左/向右滑动更改viewpager的页面/片段时它工作正常并将标签的文本颜色从正常更改为突出显示,反之亦然。
但是,当我点击任何标签更改片段时,片段会发生变化,但标签文本仍保持选中状态。当用户触摸另一个选项卡时,应该取消选中。这是我的设置选项卡布局java函数
private void setupTabLayout() {
tabLayout.setTabTextColors(R.color.LightGrey,R.color.app_highlight_color);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
//tab.setCustomView(R.drawable.tab_trending_selected);
switch (tab.getPosition()) {
case AppConstants.TRENDING_INDEX:
tab.setIcon(R.drawable.tab_trending_selected);
tab.select();
break;
case AppConstants.DUB_INDEX:
tab.setIcon(R.drawable.tab_dub_selected);
tab.select();
break;
case AppConstants.PROFILE_INDEX:
tab.setIcon(R.drawable.tab_profile_selected);
tab.select();
break;
default:
break;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
switch (tab.getPosition()) {
case AppConstants.TRENDING_INDEX:
tab.setIcon(R.drawable.tab_trending_unselected);
//tab.setText("Trending");
break;
case AppConstants.DUB_INDEX:
tab.setIcon(R.drawable.tab_dub_unselected);
// tab.setText("Dub");
break;
case AppConstants.PROFILE_INDEX:
tab.setIcon(R.drawable.tab_profile_unselected);
// tab.setText("Profile");
break;
default:
break;
}
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
tabLayout.setTabTextColors(getResources().getColor(R.color.Gray), getResources().getColor(R.color.app_highlight_color));
tabLayout.getTabAt(AppConstants.TRENDING_INDEX).setIcon(R.drawable.tab_trending_selected);
tabLayout.getTabAt(AppConstants.DUB_INDEX).setIcon(R.drawable.tab_dub_unselected);
tabLayout.getTabAt(AppConstants.PROFILE_INDEX).setIcon(R.drawable.tab_profile_unselected);
tabLayout.getTabAt(AppConstants.TRENDING_INDEX).select();
}
答案 0 :(得分:0)
面临独特的问题。当我们设置setCurrentItem时。它不会更改tablayout的选项卡。然后你必须在viewpager上添加onOPageChangeListener,你必须手动为所选的viewpager的位置选择tablayout的选项卡。然后setupWithViewPager。
使用您的viewPage添加此代码,这对我有用。
viewPager.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) {
}
});
/*
NOTE: This is setup after addOnPageChangeListener. Don't know why but this is what works. Otherwise tabLayout.does not select.
*/
tabLayout.setupWithViewPager(this.viewPager);
答案 1 :(得分:0)
试试这个:
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int pos) {
// set the title
ab.setSelectedNavigationItem(pos);
ab.setTitle(titles[pos]);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
答案 2 :(得分:0)
使用这样的东西。
<style name="CustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">@color/colorAccent</item>
<item name="tabSelectedTextColor">@color/colorAccent</item>
<item name="tabTextAppearance">@style/CategoryTabTextAppearance</item>
<item name="tabBackground">@color/colorPrimary</item>
</style>
<style name="CategoryTabTextAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textColor">@color/secondaryText</item>
</style>