我在一个冰淇淋三明治应用程序中使用了已被弃用的TabHost / TabWidget库。 我没有时间熟悉ActionBarSherlock,所以我只能按照我知道的方法进入应用程序。
我想知道如何设置TabWidget及其标签对象的样式,从更改选定的标签颜色到背景颜色和图像? 使用标准的样式和主题似乎不起作用。
我在一个冰淇淋三明治应用程序中使用了已被弃用的TabHost / TabWidget库。 我没有时间熟悉ActionBarSherlock,所以我只能按照我知道的方法进入应用程序。
我想知道如何设置TabWidget及其标签对象的样式,从更改选定的标签颜色到背景颜色和图像? 使用标准的样式和主题似乎不起作用。
假设废弃的库与以往一样正常工作,按照应有的方式工作,这是我用来着色标签的过程。由于在xml中无法直接访问背景,所以我只需在代码中设置背景:
TabWidget tabs = (TabWidget)getTabWidget();
for (int i = 0; i<tabs.getChildCount(); i++) {
RelativeLayout tab = (RelativeLayout) tabs.getChildAt(i);
tab.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.tabindicator));
<?xml version="1.0" encoding="utf-8" ?>
<selector 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/tab_unselected" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_focus" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_focus" />
<!-- Pressed -->
<item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_press" />
<item android:state_pressed="true" android:drawable="@drawable/tab_press" />
</selector>
这些可绘制对象只是带有颜色的9-patch图像,虽然您可以使用标准颜色获得类似的效果。
FragmentTabHost
后,这个又被再次使用起来。此外,在FragmentTabHost
中,选项卡子视图不是RelativeLayout
而是LinearLayout
。 - Abdullah Shoaib