2014年2月10日 星期一

Android 開發 (二十五) how to change bottom color of tabhost

需求

如何更改tabhost 底下那條線的顏色,我花了不少時間尋找可用的方法,可惜一直找不到較好的方法,最後的方法是自己製作圖然後更換背景圖,效果比想像的還要好。
如下圖



實現方法

首先先實做兩個背景圖

line_label_1_pressed.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <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>
</layer-list>

line_label_1.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

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

</layer-list>



接著使用selector
<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"/>
</selector>


接著只需要在 view 裡面使用這個背景即可, 範例如下

android:background="@drawable/tab_line_selector_bottom"

當被selected的時候就會顯示, line_label_1_pressed 其他時候就會使用line_label_1的xml。

沒有留言:

張貼留言