android開發training之actionbar之3

裝飾action bar


一、使用默認的action bar

Android theme有兩種theme支持action bar


可以在<application> 中定義應用的全局theme也可以在<activity> 中單獨定義一個activity的theme。例如:

<application android:theme="@android:style/Theme.Holo.Light" ... />

效果如下:


action bar是黑色而其餘部分是light color使用Theme.Holo.Light.DarkActionBartheme.

當使用支持庫時,用下面的方法:

When using the Support Library, you must instead use the Theme.AppCompat themes:


二、自己定製的action bar

Android3.0及以上:

像下面這樣改變action bar的背景色:


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/orange</item>
    </style>
</resources>

在整個app或某個activity中應用主題:

<application android:theme="@style/CustomActionBarTheme" ... />
android2.1以下就不指明怎麼用了。

三、自定義action bar中文字。

實現如下效果:


<?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/orange</item>
        <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>
    </style>


    <!-- ActionBar title text -->
    <style name="MyActionBarTitleText" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
        <item name="android:textColor">@color/actionbar_text</item>
    </style>
</resources>

android2.1以下就不指明怎麼用了。

四、將action bar變成透明的效果,如下:


Android 3.0以上,自己定製的theme的父theme應該是Theme.Holo 例如:


<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
           parent="@android:style/Theme.Holo">
        <item name="android:windowActionBarOverlay">true</item>
    </style>
</resources>
但是會有一個問題,頂部的textview在action bar下面,用下面的方法解決。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?android:attr/actionBarSize">
    ...
</RelativeLayout>

http://download.csdn.net/detail/xliubaox/8304183  源代碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章