TabWidget当前标签底线颜色

我有一个TabWidget,我已经启用并设置stripLeftstripRight

 mTabHost.getTabWidget().setStripEnabled(true); mTabHost.getTabWidget().setRightStripDrawable(R.drawable.redline); mTabHost.getTabWidget().setLeftStripDrawable(R.drawable.redline); 

如下图所示,这不会更改当前选定选项卡的底线颜色(TAB 2)。

在这里输入图像说明

如何将当前选中的选项卡的底线颜色更改为蓝色? (我猜猜蓝色是在styles.xml的默认AppTheme风格中设置的。)

我看了这个答案,但没有说如何改变颜色…

标签指示器的颜色由select器drawable设置,可以在这里find,如下所示:

 <!-- AOSP copyright notice can be found at the above link --> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Non focused states --> <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_holo" /> <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected_holo" /> <!-- Focused states --> <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_focused_holo" /> <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected_focused_holo" /> <!-- Pressed --> <!-- Non focused states --> <item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_holo" /> <item android:state_focused="false" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_holo" /> <!-- Focused states --> <item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_holo" /> <item android:state_focused="true" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_holo" /> </selector> 

select器使用的drawable全部用浅蓝色着色。 你可以用你自己的着色版本replace那些可绘制的。 原件看起来像这样(原件很小,包括链接):

  • tab_unselected_holo tab_unselected_holo
  • tab_selected_holo tab_selected_holo
  • tab_unselected_focused_holo tab_unselected_focused_holo
  • tab_selected_focused_holo tab_selected_focused_holo
  • tab_unselected_pressed_holo tab_unselected_pressed_holo
  • tab_selected_pressed_holo tab_selected_pressed_holo

你会想把上面的select器和drawables一起复制到你自己的项目中。 然后,您需要将可绘制的颜色重新着色成您想要的任何颜色。 然后,您需要将select器设置为选项卡指示器的背景。 你可以这样做(设置你的标签后):

 TabHost host = (TabHost)view.findViewById(R.id.tab_host); TabWidget widget = host.getTabWidget(); for(int i = 0; i < widget.getChildCount(); i++) { View v = widget.getChildAt(i); // Look for the title view to ensure this is an indicator and not a divider. TextView tv = (TextView)v.findViewById(android.R.id.title); if(tv == null) { continue; } v.setBackgroundResource(R.drawable.your_tab_selector_drawable); } 

有一个更简单的方法来做到这一点,通过设置自己的客户指标布局与背景select器,但这是最简单的工作。

这是我改变我的标签,

 private void changetabs(TabWidget tabWidget) { // Change background for(int i=0; i < tabWidget.getChildCount(); i++) tabWidget.getChildAt(i).setBackgroundResource(R.drawable.tab_selector); } 

和我的tab_selector.xml

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Non focused states --> <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_holo" /> <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected_holo" /> <!-- Focused states --> <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_focused_holo" /> <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected_focused_holo" /> <!-- Pressed --> <!-- Non focused states --> <item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_holo" /> <item android:state_focused="false" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_holo" /> <!-- Focused states --> <item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_holo" /> <item android:state_focused="true" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_holo" /> 

希望它能帮助一些人。

有一个人绊倒它,有在线工具来快速构build标签的可绘制(9补丁)。 只要select颜色,然后按下button在这里你去…

感谢Jeff Gilfelt


Android操作栏风格生成器允许您轻松地为您的Android应用程序创build一个简单,有吸引力和无缝的自定义操作栏风格。 它将生成所有必需的九个修补程序资产以及可以直接复制到项目中的关联的XML可绘制和样式。

http://jgilfelt.github.io/android-actionbarstylegenerator/

你可以使用app:tabIndicatorColor来达到这个目的。 它会根据您的要求更改选定的标签指示线颜色。

 <android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabIndicatorColor="@android:color/white" app:tabMode="fixed" /> 

你可以使用一个filter,
这将适用于不透明的地区

 tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).getBackground().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY); 

一行代码 – 不需要改变状态。

强调颜色默认用作活​​动标签颜色。 您可以在style.xml文件中设置/更改它:

 <style name="AppBaseTheme" parent="Theme.AppCompat.Light"> <item name="colorAccent">@color/myAccentColor</item> </style> 

我解决这个问题的方法是使用setBackgroundResource。 首先,你必须创build完全相同的背景

line_label_1_pressed.xml

 <item android:top="-6dp" android:left="-6dp" android:right="-6dp"> <shape> <size android:height="50dp"/> <solid android:color="@android:color/transparent"/> <stroke android:color="@color/myColor" android:width="6dp"/> </shape> </item> 

line_label_1.xml

 <item> <shape> <solid android:color="@android:color/transparent" /> </shape> </item> 

然后如下创buildtab_selector.xml

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/line_label_1_pressed" android:state_selected="true"/> <item android:drawable="@drawable/line_label_1"/> 

然后使用tab_selector.xml设置setbackgroudResource

 <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/tab_selector" android:gravity="center_horizontal|center_vertical" /> 

我find了其他解决scheme,打开styles.xml并更改一行:

res – > values – > styles.xml

 <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@android:color/holo_orange_light</item> <!-- set the color in this line --> <item name="windowNoTitle">true</item> </style> 

只要使用类似的东西

 tabHost.setSelectedTabIndicatorColor(Color.WHITE);