Android ActionBar自訂

來源:互聯網
上載者:User

標籤:

1.設定action bar的背景色 

第一種方式:getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.ActionBarBackground));

ActionBarBackground 為定義在values/colors.xml

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="ActionBarBackground">#ED4026</drawable>
</resources>

此種方法特點:簡單方便,但必須在代碼中設定,若不同activity公用同一個actionbar樣式,則不利於複用 。 so, 參考第二種方式

 

第二種方式:由官方文檔給出,通過設定主題的屬性實現。(不同activity可複用)

具體做法: (以下方法適合3.0及以上,2.1及以上參考官方文檔)

res/values/themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
           parent="@android:style/Theme.Holo.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
           parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@drawable/actionbar_background</item>
    </style>
</resources>

Then apply your theme to your entire app or individual activities:
<application android:theme="@style/CustomActionBarTheme" ... />
也可在建立項目後application或activity預設引用的主題樣式檔案styles.xml中直接修改添加
修改 <style name="AppBaseTheme" parent="@android:style/Theme.Holo"> 
或添加以上檔案內容到styles.xml
actionbar_background可添加在colors.xml檔案中

 

 

參考:http://blog.csdn.net/jwzhangjie/article/details/28852967

參考:http://blog.csdn.net/yueqinglkong/article/details/20876401

Android ActionBar自訂

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.