Styling tabBar-indicator with ActionBarSherlock

2019-07-14 06:59发布

问题:

I am currently not able to customize the tab-indicator in my app when using Actionbarsherlock. I am trying to change the default color (blue) to white of the indicator. All other customization works as intended. I am not even able to remove the tab-indicator. Have I missed something in my XML-code or what might be the problem? Also note, my min-SDK is set to 14 in the manifest -file. ALL help is needed so I can manage to find the source of the problem :/ Regards

Tabs.java

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_host);

ActionBar actionBar = getSupportActionBar();
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            actionBar.setDisplayShowTitleEnabled(true);
            actionBar.setDisplayShowHomeEnabled(true);
            actionBar.setTitle(R.string.LabelTitleAbout);


            actionBar.addTab(actionBar.newTab().
                setTabListener(new TabListener<LandingSearch>(this, getString(R.string.LabelSearchTabTitle), LandingSearch.class, null)));

the code continues with the constructor etc...

tab_host.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <LinearLayout
        android:id="@+id/tabs_navigation_content"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </LinearLayout>
</LinearLayout>

values/themes.xml

<style name="Theme.ML" parent="@style/Theme.Sherlock">
    <item name="windowNoTitle">true</item>
    <item name="android:actionBarTabBarStyle">@style/Theme.ML.Tabs</item>
     <item name="actionBarTabBarStyle">@style/Theme.ML.Tabs</item>
</style>

<style name="Theme.ML.Tabs" parent="Widget.Sherlock.Light.ActionBar.Solid.Inverse">
    <item name="windowNoTitle">true</item>
    <item name="background">@drawable/tab_bar_background</item> 
    <item name="android:background">@drawable/tab_bar_background</item>

</style>
...

drawable/tab_bar_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/menu_blank_off"/>
  <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_bar_background_selected"/>
  <item android:state_selected="false" android:state_pressed="true" android:drawable="@color/white"/>
  <item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_bar_background_selected_pressed"/>
</selector>

tab_bar_background_selected.xml

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="-5dp" android:left="-5dp" android:right="-5dp">
        <shape android:shape="rectangle">
            <stroke android:color="#ffffff" android:width="5dp"/>

        </shape>
    </item>
</layer-list>

回答1:

You can fully customized action bar style using action bar style generator here a link for action bar style generator: Action Bar Style Generator

also you can do from this Example:- Customize Action bar



回答2:

As Vikram said you already using API 14+ why to use ActionBarSherlock over default action bar,

One can customise ActionBar with different variation using Android Action Bar Style Generator.

Why ABS Tab indicator not changing?

As you said your action bar tab indicator color is not get changed, here is why one need to styling attributes where it should belongs to.

<style name="Theme.ML.Tabs" parent="Widget.Sherlock.Light.ActionBar.Solid.Inverse">

is not right place to style your Action Bar Tabs.

Solution:-

<style name="Theme.ML" parent="@style/Theme.Sherlock">
    <item name="windowNoTitle">true</item>
    <item name="android:actionBarTabStyle">@style/Theme.ML.Tabs</item>
    <item name="actionBarTabBarStyle">@style/Theme.ML.Tabs</item>
</style>

<style name="Theme.ML.Tabs" parent="@style/Widget.Sherlock.Light.ActionBar.TabView">
        <item name="android:background">@drawable/tab_bar_background</item>
</style>