res/drawable/tabselector.xml
<selector
android:id="@+id/myselector"
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/darklogo" />
<item
android:state_focused="false"
android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/lightlogo" />
<!-- Focused states -->
<item
android:state_focused="true"
android:state_selected="false"
android:state_pressed="false"
android:drawable="@drawable/lightlogo" />
<item
android:state_focused="true"
android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/lightlogo" />
<!-- Pressed -->
<item
android:state_pressed="true"
android:drawable="@drawable/lightlogo" />
</selector>
您在此处包含的 XML 是一种定义可绘制对象的方法,可让您嵌入 case 语句。它根据分配给它的视图的状态呈现不同的可绘制对象。作为可绘制对象,您应该将其保存为res/drawable
项目文件夹中的 xml 文件(例如tabselector.xml
)。
要将它用于 Tabhost,您需要像往常一样构造 TabActivity(如本教程示例所示)。
然后,当您将每个选项卡添加到主机时,您将tabselector
可绘制对象指定为指示器,如下面的“TAB 1”所示。
Drawable mySelector = getResources().getDrawable(R.drawable.tabselector);
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1", mySelector).setContent(R.id.textview1));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(R.id.textview2));