在android TabHost layout
上,当用户选择标签时,标签的颜色会暂时改变。如何禁用此颜色更改,或指定选项卡更改为的颜色?
答案 0 :(得分:6)
答案 1 :(得分:1)
我建议您实现自己的tab_indicator_selector.xml。
示例:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states ... -->
<!-- Focused states ... -->
<!-- Pressed -->
<item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_pressed" />
<item android:state_pressed="true" android:drawable="@drawable/tab_pressed" />
</selector>
这是tab_pressed.xml状态选择器:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="53dp">
<shape android:shape="rectangle">
<solid android:color="@color/white" />
</shape>
</item>
<item android:top="53dp" android:bottom="1dp">
<shape android:shape="rectangle">
<solid android:color="@color/white" />
</shape>
</item>
<item android:left="2dp" android:right="2dp">
<shape android:shape="rectangle">
<gradient android:angle="90"
android:startColor="@color/black"
android:endColor="@color/white" />
<stroke android:width="2dp" android:color="@color/red" />
</shape>
</item>
</layer-list>
其他状态可以通过创建tab_focused.xml,tab_selected.xml和tab_unseleceted.xml并设置正确的android:state_XXX组合来实现,整个标签背景的颜色由元素给出,但是你可以使用实体矩形的颜色。
自定义tab_indicator(没有图像)如下所示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="52dip"
android:layout_weight="1"
android:orientation="vertical"
android:background="@drawable/tab_indicator_selector"
android:layout_marginTop="2dp"
android:padding="2dp">
<TextView android:id="@+id/tab_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="11dp"
android:textStyle="bold"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
style="?android:attr/tabWidgetStyle"
/>
</RelativeLayout>