ActionBar详解之三(自定义样式)

来源:互联网 发布:js target.nodename 编辑:程序博客网 时间:2024/06/01 07:59

1.使用自带主题定义样式

不需要兼容时:
在androidManifest.xml文件中定义主题:
<application android:theme="@android:style/Theme.Holo.Light" ... />样式如下:

           
<application android:theme="@android:style/Theme.Holo" ... />样式如下:

<application android:theme="@android:style/Theme.Holo.Light.DarkActionBar".../>样式如下:


需要兼容时:
以上三种写法分别改为:
<application android:theme="@style/Theme.AppCompat.Light" ... />
<application android:theme="@style/Theme.AppCompat" ... />
<application android:theme="@style/Theme.AppCompat.Light.DarkActionBar" ... />
效果不变。

2.自定义背景

为了修改ActionBar的背景颜色,我们为activity自定义一个主题,该主题覆盖了已有的主题,我们在自定义主题中设置背景资源。

对于android3.0及更高的版本:
<?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>
对于android2.0及更高的版本:
<?xml version="1.0" encoding="utf-8"?><resources>    <!-- the theme applied to the application or activity -->    <style name="CustomActionBarTheme"           parent="@style/Theme.AppCompat.Light.DarkActionBar">        <item name="android:actionBarStyle">@style/MyActionBar</item>        <!-- Support library compatibility -->        <item name="actionBarStyle">@style/MyActionBar</item>    </style>    <!-- ActionBar styles -->    <style name="MyActionBar"           parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">        <item name="android:background">@drawable/actionbar_background</item>        <!-- Support library compatibility -->        <item name="background">@drawable/actionbar_background</item>    </style></resources>
当然,二者都需在清单文件中作如下配置:
<application android:theme="@style/CustomActionBarTheme" ... />

3.使用分离式ActionBar

分离式ActionBar将actions的条目显示在屏幕底部,在屏幕比较狭窄时,为屏幕顶端留下了空间。
我们在Activity的配置中添加如下语句:
android:uiOptions="splitActionBarWhenNarrow"
对于api14及其以上版本有效,API14以下,会自动忽略。
为了支持旧版本,我们也可以采用meta-data的格式:
 <meta-data android:name="android.support.UI_OPTIONS"
                   android:value="splitActionBarWhenNarrow" />
即:
<manifest ...>    <activity uiOptions="splitActionBarWhenNarrow" ... >        <meta-data android:name="android.support.UI_OPTIONS"                   android:value="splitActionBarWhenNarrow" />    </activity></manifest>
效果如图:








  
0 0
原创粉丝点击