[英]Unable to style ActionBar tab indicator using AppCompat
我一直在嘗試使用AppCompat庫自定義Android ActionBar選項卡上的藍色指示器,但未成功。 我可以更改標簽欄的背景顏色,但是看起來themes.xml完全忽略了android:actionBarTabStyle
屬性。 我定義了一組9個補丁圖像,一個基於狀態的可繪制XML文件。
搜索Google仍無法解決我的問題,我想使用盡可能少的依賴項,因此除非萬不得已,否則我不願使用ActionBarSherlock。
預期結果:使用9色塊圖像,選項卡指示器應為橙色。
實際結果:選項卡指示器仍為全息藍色
tab_indicator.xml
<?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_orange" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected_orange" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_focused_orange" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected_focused_orange" />
<!-- Pressed -->
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_orange" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_orange" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_orange" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_orange" />
</selector>
的themes.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="style/Theme.AppCompat">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:actionBarStyle">@style/ActionBarTheme</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">@style/ActionBarTheme</item>
</style>
<!-- ActionBar styles -->
<style name="ActionBarTheme" parent="@style/Widget.AppCompat.ActionBar">
<item name="android:background">@color/titleBackgroundColour</item>
<item name="android:backgroundStacked">@color/tabBackground</item>
<item name="android:textColor">@color/titleTextColour</item>
<item name="android:titleTextStyle">@style/ActionBarTitleTextStyle</item>
<item name="android:actionBarTabStyle">@style/ThemeTabStyle</item>
<!-- Support library compatibility -->
<item name="actionBarTabStyle">@style/ThemeTabStyle</item>
</style>
<!-- ActionBar tabs styles -->
<style name="ThemeTabStyle" parent="@style/Widget.AppCompat.ActionBar.TabView">
<!-- tab indicator -->
<item name="android:background">@drawable/tab_indicator</item>
<!-- Support library compatibility -->
<item name="background">@drawable/tab_indicator</item>
</style>
</resources>
誰能看到此代碼有任何明顯的問題,為什么它不起作用? 非常感謝。
我遇到了同樣的問題,當我從常規代碼中拆分支持兼容性代碼時,它起作用了。 將常規代碼放在目錄values-v14/themes.xml
。 這將使版本14及更高版本使用正則聲明(例如android:background
),而版本14及以下版本使用支持compat代碼(例如background
)。 希望這足夠清楚
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.