Android 下的 Actionbar 自定义风格

由于要在Android下做导航的效果 ActionBar 就成了学习的对象,在用ActionBar时遇到的一点问题,记录一下(这里只考虑4.0以上的系统,即14以上)

想要做的效果:


如上所展示,第一行是ActionBar,第二行是3个ActionBar Tab

ActionBar 和 ActionBarTab 的自定义风格步骤:

0.文件目录概览


1.首先是 res/values/styles.xml 文件

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    
    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        
    </style>
    
    <style name="AppBaseTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/FMPActionBarStyle</item>
    	<item name="android:actionBarTabStyle">@style/FMPActionBarTabStyle</item>
    	<item name="android:actionBarTabBarStyle">@style/FMPActionBarTabBarStyle</item>
    </style>
    
    <!-- ActionBar styles -->
    <style name="FMPActionBarStyle"
        parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@color/actionbar_background</item>
    </style>
    
    <!-- ActionBar Tab styles -->
    <style name="FMPActionBarTabStyle"
        parent="@android:style/Widget.Holo.ActionBar.TabView"
        >
        <item name="android:background">@drawable/actionbar_tab_indicator</item>
    </style>
    
    <!-- ActionBar Tab Bar styles -->
    <style name="FMPActionBarTabBarStyle"
        parent="@android:style/Widget.Holo.ActionBar.TabBar"
        >
        <item name="android:showDividers">none</item>
    </style>

</resources>

2.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.@drawable/tab_unselected 类的文件,这里展示 /drawable/tab_unselected.xml 的代码

<?xml version="1.0" encoding="utf-8"?>
<color xmlns:android="http://schemas.android.com/apk/res/android" 
    android:color="@color/actionbar_tab_unselected_unpressed">
</color>
这个文件的编写涉及 drawable 资源的知识,可参考官方文档  http://developer.android.com/intl/zh-cn/guide/topics/resources/drawable-resource.html

或中文博客 http://blog.csdn.net/lonelyroamer/article/category/1221987 里关于drawable 的知识博客


4.colors.xml 文件的编码

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="actionbar_background">#f00</color>
    <color name="actionbar_tab_unselected_unpressed">#303030</color>
    <color name="actionbar_tab_unselected_pressed">#404040</color>
    <color name="actionbar_tab_selected_unpressed">#404040</color>
    <color name="actionbar_tab_selected_pressed">#505050</color>
</resources>

5.当然,还要在 Manifest 里面添加 android:theme 指向你的主题,即 1 定义的 AppTheme

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity>
            <!-- ... -->
        </activity>
    </application>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值