[英]how to move tab text color like indicator?
I have a tab layout with a custom layout for the tabs I want the text to be white when the tab is selected and grey when its not, I've achieved this with a state selector and it works perfectly when I slide from tab to tab or when I click on one, however I want to dynamically change the tab on a button click from a fragment so I'm using this: 我有一个带有自定义布局的选项卡布局,我希望选项卡处于选中状态时文本为白色,否则为灰色;通过状态选择器实现了此目的,当我在选项卡之间滑动时,它可以完美地工作或当我单击一个按钮时,但是我想动态地更改片段中单击按钮的选项卡,因此我正在使用以下命令:
MainActivity.viewPager.setCurrentItem(2);
when I use this to change the tab my text on the tab doesnt change but the indicator does, 当我使用它来更改选项卡时,选项卡上的文本不会更改,但指示器会更改,
so I tried setting up a switch (see below) and calling it when I change tabs like this: 所以我尝试设置一个开关(如下所示)并在我更改标签时调用它:
MainActivity.viewPager.setCurrentItem(2);
MainActivity.tabTitleColor();
and this works the tab I've moved to has white text and the tab I've moved from's text is grey, but if I click another tab after using my switch I get two tabs with white text (strangely swiping doesn't have this issue and works perfectly), the indicator is always in the correct position so I wonder if any one knows if I can get its (the indicators) position, I'm still new to all this so maybe there's a simpler answer 这有效,我移至的选项卡具有白色文本,而我从其移出的选项卡为灰色,但是如果我在使用开关后单击另一个选项卡,则会得到两个带有白色文本的选项卡(奇怪地刷卡没有此选项)问题,并且运行良好),指标始终处于正确的位置,因此我想知道是否有人知道我能否获得其(指标)位置,所以我对这一切还是陌生的,所以也许有一个更简单的答案
many thanks 非常感谢
heres my switch.... just in CASE! 这是我的开关。。。 hahaha 哈哈哈
public static void tabTitleColor(){
int position2 = viewPager.getCurrentItem();
switch (position2){
case 0:
tabLayout.getTabAt(0).getCustomView().setSelected(true);
tabLayout.getTabAt(1).getCustomView().setSelected(false);
tabLayout.getTabAt(2).getCustomView().setSelected(false);
tabLayout.getTabAt(3).getCustomView().setSelected(false);
System.out.println("tab "+ position2);
break;
case 1:
tabLayout.getTabAt(1).getCustomView().setSelected(true);
tabLayout.getTabAt(0).getCustomView().setSelected(false);
tabLayout.getTabAt(2).getCustomView().setSelected(false);
tabLayout.getTabAt(3).getCustomView().setSelected(false);
System.out.println("tab "+ position2);
break;
case 2:
tabLayout.getTabAt(2).getCustomView().setSelected(true);
tabLayout.getTabAt(1).getCustomView().setSelected(false);
tabLayout.getTabAt(0).getCustomView().setSelected(false);
tabLayout.getTabAt(3).getCustomView().setSelected(false);
System.out.println("tab "+ position2);
break;
case 3:
tabLayout.getTabAt(3).getCustomView().setSelected(true);
tabLayout.getTabAt(1).getCustomView().setSelected(false);
tabLayout.getTabAt(2).getCustomView().setSelected(false);
tabLayout.getTabAt(0).getCustomView().setSelected(false);
System.out.println("tab "+ position2);
break;
}
}
adding my selector as requested it lives in a folder named color 根据要求添加我的选择器,它位于一个名为color的文件夹中
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@android:color/white" />
<item android:state_focused="true" android:color="@android:color/white" />
<item android:state_pressed="true" android:color="@android:color/white" />
<item android:color="#d1c9c9" />
</selector>
I managed to solve this in the end by scrapping my custom view, selector, and switch and using some custom styles and changing the way i was changing tabs so my styles.xml looks like this 最后,我设法通过取消自定义视图,选择器和开关并使用一些自定义样式并更改了更改标签的方式来解决此问题,因此我的styles.xml看起来像这样
<style name="CustomTabLayoutStyle" parent="Base.Widget.Design.TabLayout">
<item name="tabSelectedTextColor">@color/selected_text</item>
<item name="tabIndicatorColor">@color/colorAccent</item>
<item name="tabTextAppearance">@style/CustomTabTexStyle</item>
</style>
<style name="CustomTabTexStyle" parent="TextAppearance.Design.Tab">
<item name="android:textColor">@color/unselected_text</item>
<item name="textAllCaps">true</item>
</style>
added it to my tablayout like this 像这样将其添加到我的布局中
style="@style/CustomTabLayoutStyle"
and then changed the way i was moving tabs to this 然后改变了我将标签移动到此的方式
TabLayout tabLayout = MainActivity.tabLayout;
TabLayout.Tab tab = tabLayout.getTabAt(0);
tab.select();
and everything is now hunky dorey 现在一切都变态了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.