简体   繁体   中英

How to set ActionBar background

All I want to do is a ActionBar with white background and a 1px red border at bottom. How can I achieve this?

Note: I have tried using 9-patch but red line always had some gradient BUT I don't want any gradient. Here is my 9-patch (T:Transparent, B:Black, W:White, R:Red):

TBT
BWB
TRT
TBT

As a best practice, should I define a separate xml that includes two rectangle shapes?

You don't need a nine-patch for that. You can just define a drawable in XML, like this:

<?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="2dp">
        <shape android:shape="rectangle">
            <solid android:color="#FFFFFF" />
        </shape>
    </item>
</layer-list>

Then just use getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.mydrawable));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM