【发布时间】:2013-08-11 21:27:56
【问题描述】:
我希望 ActionBar 中的所有选项卡都有不同的颜色指示器,例如选项卡 1 为蓝色,选项卡 2 为红色等。
为了实现这一点,我确实为所有颜色创建了不同的选择器,并将它们放在可绘制的不同 xml 中。在 style.xml 中,我通过
调用它们<style name="MyTheme" parent="AppBaseTheme">
<item name="android:actionBarTabStyle">@style/ActionBarTabStyleRed</item>
</style>
<style name="ActionBarTabStyleRed">
<item name="android:background">@drawable/tab_indicator_red</item>
</style>
我也为不同的颜色创建了这种样式。现在,当我将可绘制对象或样式更改为不同的颜色时,它可以工作了。我可以看到颜色被应用于选项卡。但由于所有标签都是相同的颜色,它并没有解决我的目的。我试图在onTabSelected() 中设置标签样式,但没有办法做到这一点。
最终我尝试为不同的颜色创建不同的主题并尝试在onTabSelected() 中以编程方式设置它们,但后来我知道必须在setContentView() 之前设置主题。
所以我的问题是..我该怎么做?有什么办法可以为不同的标签指示器设置不同的颜色???
更新:-
drawable/tab_indicator_red.xml
<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="@android:color/transparent" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected_red" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_focused_red" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected_focused_red" />
<!-- Pressed -->
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_red" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_red" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_red" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_red" />
</selector>
【问题讨论】:
-
我有一个,但我必须清除一些东西-> 在选择器中,你有什么代替“...”?您是否对不同的颜色使用不同的样式?你如何使用样式。如果所有选项卡都必须是不同的颜色,你如何分别设置它们(或者这就是这里的问题)?
-
@g00dy 更新了问题的更多细节。你没看错,这就是这里的问题。我对所有可能导致不同选项卡的颜色指示符不同的方式持开放态度,无论是通过 xml、选择器还是通过 java 代码。我更喜欢最不麻烦的方式。 :-)
标签: android tabs android-actionbar android-3.0-honeycomb