android 快速更改TabLayout的选中背景颜色。

393 阅读1分钟

tab_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/tab_background_unselected" android:state_pressed="false"/>
    <item android:drawable="@drawable/tab_background_selected" android:state_selected="true" />
</selector>

tab_background_selected.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#09000000" />
</shape>

\

tab_background_unselected.xml\

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#3F51B5" />
</shape>

上面的tab_background.xml中写了tab的选中与不选中是的两种状态;分别设置不同的颜色就行。

最好需要新建一个tablayout的style:

<style name="zhangjiqunStyle" parent="android:Widget">
    <item name="tabBackground">@drawable/tab_background</item>
    <item name="tabIndicatorColor">#ff00ff</item>
    <item name="tabIndicatorHeight">2dp</item>
</style>

这个style你要单独的去命名,我的名字叫张继群就随便的命名了。在你的style中有tablayout单独的style;名称如下:

Base.Widget.Design.TabLayout 这个style很特殊。专门为tablayout准备。

这样就完成了。\