1. 程式人生 > >android 快速更改TabLayout的選中背景顏色。

android 快速更改TabLayout的選中背景顏色。

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準備。

這樣就完成了。