Android: Change color of line below ActionBar

2019-01-18 07:09发布

问题:

I want to change the color of the orange line below the ActionBar.

I've tried a few things but nothing has helped so far.

The current theme on my Galaxy S2 is an orange theme. So changing the theme, also changes the line. But I don´t know how to get access to that line, in order to change the color.

回答1:

You can use This code for achieving your purpose.

background.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#ff0000" />
        </shape>
    </item>
    <item android:bottom="5dp">
        <shape android:shape="rectangle">
            <solid android:color="#ffffff" />
        </shape>
    </item>
</layer-list>

styles.xml

<resources>
    <style name="AppTheme" parent="android:Theme.Holo">
        <item name="android:actionBarStyle">@style/AppTheme.ActionBar</item>
        <item name="android:background">#ffffff</item>
        <item name="android:colorBackground">#ffffff</item>
    </style>
    <style name="AppTheme.ActionBar" parent="android:Widget.ActionBar">
        <item name="android:background">@drawable/background</item>
    </style>
</resources>

These codes are suitable when you have not any options menu like image shows in question, if you have options menu you must customize your option menu items background.

If you have Option menu below code is suitable:

<resources>
    <style name="AppTheme" parent="android:Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/AppTheme.ActionBar</item>
        <item name="android:popupMenuStyle">@style/AppTheme.PopUpMenu</item>
    </style>
    <style name="AppTheme.ActionBar" parent="android:Widget.ActionBar">
        <item name="android:background">@drawable/background</item>
    </style>
    <style name="AppTheme.PopUpMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
        <item name="android:popupBackground">@drawable/background</item>
    </style>
</resources>


回答2:

There is A simpler way than this (Creating Bunch of Styles). Refering to this . Just set

getActionBar().setBackgroundDrawable(null);

This will Remove the line from Your Action Bar



回答3:

I used this ^_^ and it is 100% working:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Drawable res = getResources().getDrawable(R.drawable.ab_transparent_example);
    getActionBar().setBackgroundDrawable(res);

and JUST USE THIS



回答4:

In my case, in my application, I used the Holo theme.

<style name="AppTheme" parent="android:Theme.Holo">
       ...
 </style>

Background toolbar is set through the property.

<style name="AppTheme" parent="android:Theme.Holo">
    <item name="android:actionBarStyle">@style/ActionBarStyle</item>
</style>

<style name="ActionBarStyle" parent="@android:style/Widget.Holo.ActionBar">
    <item name="android:background">@drawable/action_bar_background</item>
</style>

Standard Holo action bar background is nine-patch drawable like this:
nine patch drawable

So, if you want to change the color of the bottom line, you need to create a new nine-patch drawable. Create Resizable Bitmaps (9-Patch files)

Put the file in the folder drawable (example drawable-xxhdpi) and specify the path to the file in the theme.