How to create an actionbar, the background of a tab on a selected tab

I am struggling with the design of ActionBar. My application has an ActionBar with three tabs. I am trying to make the selected tab have a background color, and unselected tabs display a different color. I follow this link: Custom action bar . But all TABs display the selected color.

My styles.xml file looks like this:

 <style name="MyActionBarTabStyle" parent="android:style/Widget.Holo.Light.ActionBar.TabBar"> <item name="android:background">@drawable/tab_background</item> <item name="android:paddingLeft">32dp</item> <item name="android:paddingRight">32dp</item> </style> <style name="MyActionBarTabBarStyle" parent="android:style/Widget.Holo.Light.ActionBar.TabBar"> <item name="android:background">@drawable/red</item> </style> <style name="AppTheme.Light" parent="@android:style/Theme.Holo.Light"> <item name="android:actionBarStyle">@style/ActionBar.Light</item> <item name="android:actionBarTabStyle">@style/MyActionBarTabStyle</item> <item name="android:actionBarTabBarStyle">@style/MyActionBarTabBarStyle</item> </style> 

tab_background - only 9 patches. I'm also not sure if I inherit the action bar tab from the correct parent ( parent="android:style/Widget.Holo.Light.ActionBar.TabBar ). I looked through the links and it is very difficult to understand the hierarchy of styles.

Why are my tabs not showing or not? Thanks in advance for your help.

+16
android android-actionbar android-styles android-tabs
Nov 08
source share
1 answer

I solved my problem. At first, I did not use State List Drawables. I used the background image directly, not through StateListDrawable. Using StateListDrawable, you can set a different background based on tag selection or not.

So I added the tab_background_select.xml file

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:drawable="@drawable/tab_background" /> </selector> 

and I referenced this with styles.xml :

  <item name="android:background">@drawable/tab_background_select</item> 
+10
Nov 08 '12 at 11:22
source share



All Articles