安卓训练-开始-添加操作栏-为操作栏添加样式

为操作栏添加样式

操作栏让用户以熟悉和可预测的方法执行操作在你的应用中导航,但那并不表示它需要看起来和其他应用完全一样。你可以很容易地使用安卓的 样式与主题 资源,给操作栏添加样式,让它更好地符合你的品牌。

安卓包含一些内建的 activity 主题,有"暗"或"亮" 操作栏样式。你可以扩展这些主题,进一步自定义你的操作栏的外观。

注意:如果你为操作栏使用支持库 API,你需要使用或覆盖 Theme.AppCompat 样式族(而不是 API 等级 11 和更高等级可用的Theme.Holo 族)。这样做时,每个样式属性你都必须声明两次:一次使用平台的样式属性(android: 属性),另一次使用支持库中包含的样式属性(appcompat.R.attr 属性—这些属性的上下文实际上是你的应用)。详情请看下面的例子。

使用安卓主题


安卓包含两个基准 activity 主题用于控制操作栏的颜色:

你可以在你的清单文件中为 <application> 元素或单独的 <activity> 元素声明 android:theme 属性来为你的整个应用或单独的 activity 应用这些主题。

例如:

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

你还可以通过声明 Theme.Holo.Light.DarkActionBar 主题使用一个暗的操作栏而 activity 的其余部分却是亮颜色的。

当使用支持库时,你必须使用 Theme.AppCompat 主题:

请确保你的操作栏的图标与操作栏的颜色形成恰当的对比。为了帮助你,操作栏图标包 包含了与 Holo 亮 和 Holo 暗操作栏一起使用的标准操作图标。

自定义背景


为了改变操作栏的背景,为你的 activity 创建一个自定义的覆盖 actionBarStyle 属性的主题。这个属性指向另一个样式,在那个样式中你可以覆盖background 属性 to 为操作栏的背景指定一个可绘制的资源。

如果你的应用使用 导航标签 或者 分裂操作栏,那你还可以相应地使用 backgroundStackedbackgroundSplit 属性为这些操作栏指定背景。

警告:声明一个合适的父主题很重要,你自定义的主题与样式继承它的样式。没有父样式,你的操作栏会没有许多样式属性,除非你自己显式地声明它们。

只支持安卓 3.0或更高版本

当只支持 3.0或更高版本时,你可以像这样定义操作栏背景:

res/values/themes.xml

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

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

然后给你的整个应用或单独的 activity 运用主题:

<application android:theme="@style/CustomActionBarTheme" ... />

支持安卓 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="@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>

然后给你的整个应用或单独的 activity 运用主题:

<application android:theme="@style/CustomActionBarTheme" ... />

自定义文本颜色


为了修改操作栏上文本的颜色,你需要为每个文本元素覆盖一些属性:

只支持安卓 3.0或更高版本

当只支持安卓 3.0或更高版本时,你的样式 XML 文件看起来可能像这样:

res/values/themes.xml

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

    <!-- ActionBar styles -->
    <style name="MyActionBar"
           parent="@style/Widget.Holo.ActionBar">
        <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>
    </style>

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

    <!-- ActionBar tabs text styles -->
    <style name="MyActionBarTabText"
           parent="@style/Widget.Holo.ActionBar.TabText">
        <item name="android:textColor">@color/actionbar_text</item>
    </style>
</resources>

支持安卓 2.1或更高版本

当使用支持库时,你的样式 XML 文件看起来可能像这样:

res/values/themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
           parent="@style/Theme.AppCompat">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>
        <item name="android:actionMenuTextColor">@color/actionbar_text</item>

        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/MyActionBar</item>
        <item name="actionBarTabTextStyle">@style/MyActionBarTabText</item>
        <item name="actionMenuTextColor">@color/actionbar_text</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
           parent="@style/Widget.AppCompat.ActionBar">
        <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>

        <!-- Support library compatibility -->
        <item name="titleTextStyle">@style/MyActionBarTitleText</item>
    </style>

    <!-- ActionBar title text -->
    <style name="MyActionBarTitleText"
           parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@color/actionbar_text</item>
        <!-- The textColor property is backward compatible with the Support Library -->
    </style>

    <!-- ActionBar tabs text -->
    <style name="MyActionBarTabText"
           parent="@style/Widget.AppCompat.ActionBar.TabText">
        <item name="android:textColor">@color/actionbar_text</item>
        <!-- The textColor property is backward compatible with the Support Library -->
    </style>
</resources>

自定义标签指示器


为了改变 导航标签 使用的指示器, 创建一个覆盖actionBarTabStyle 属性的 activity 主题。这个属性指向另一个样式资源,在那个资源中你覆盖background 属性,指定一个可绘制状态列表。

注意:一个可绘制状态列表是很重要的, 当前选中的标签使用与其他标签不同的背景表示它的状态。 更多关于怎样创建一个可以处理多个按钮状态的可绘制资源的信息,参见状态列表 文档。

例如,这里是一个可绘制状态列表,为操作栏标签的几个不同状态声明特别的背景图像:

res/drawable/actionbar_tab_indicator.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- STATES WHEN BUTTON IS NOT PRESSED -->

    <!-- Non focused states -->
    <item android:state_focused="false" android:state_selected="false"
          android:state_pressed="false"
          android:drawable="@drawable/tab_unselected" />
    <item android:state_focused="false" android:state_selected="true"
          android:state_pressed="false"
          android:drawable="@drawable/tab_selected" />

    <!-- Focused states (such as when focused with a d-pad or mouse hover) -->
    <item android:state_focused="true" android:state_selected="false"
          android:state_pressed="false"
          android:drawable="@drawable/tab_unselected_focused" />
    <item android:state_focused="true" android:state_selected="true"
          android:state_pressed="false"
          android:drawable="@drawable/tab_selected_focused" />


<!-- STATES WHEN BUTTON IS PRESSED -->

    <!-- Non focused states -->
    <item android:state_focused="false" android:state_selected="false"
          android:state_pressed="true"
          android:drawable="@drawable/tab_unselected_pressed" />
    <item android:state_focused="false" android:state_selected="true"
        android:state_pressed="true"
        android:drawable="@drawable/tab_selected_pressed" />

    <!-- Focused states (such as when focused with a d-pad or mouse hover) -->
    <item android:state_focused="true" android:state_selected="false"
          android:state_pressed="true"
          android:drawable="@drawable/tab_unselected_pressed" />
    <item android:state_focused="true" android:state_selected="true"
          android:state_pressed="true"
          android:drawable="@drawable/tab_selected_pressed" />
</selector>

只支持安卓 3.0或更高版本

当只支持安卓 3.0或更高版本时,你的样式 XML 文件看起来可能像这样:

res/values/themes.xml

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

    <!-- ActionBar tabs styles -->
    <style name="MyActionBarTabs"
           parent="@style/Widget.Holo.ActionBar.TabView">
        <!-- tab indicator -->
        <item name="android:background">@drawable/actionbar_tab_indicator</item>
    </style>
</resources>

支持安卓 2.1或更高版本

当使用支持库时,你的样式 XML 文件看起来可能像这样:

res/values/themes.xml

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

        <!-- Support library compatibility -->
        <item name="actionBarTabStyle">@style/MyActionBarTabs</item>
    </style>

    <!-- ActionBar tabs styles -->
    <style name="MyActionBarTabs"
           parent="@style/Widget.AppCompat.ActionBar.TabView">
        <!-- tab indicator -->
        <item name="android:background">@drawable/actionbar_tab_indicator</item>

        <!-- Support library compatibility -->
        <item name="background">@drawable/actionbar_tab_indicator</item>
    </style>
</resources>

更多资源

 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值