ActionBar的自定义样式

ActionBar的自定义样式

res/values,res/values-v11,res/values-v14下styles.xml全部改成相同内容。为了兼容android 2.x 修改res/values/styles.xml相关属性时去掉“android:”。

背景

“android:background”——ActionBar本身的背景。

“android:backgroundStacked”——ActionBar被分离时Tab的背景。

“android:backgroundSplit”——ActionBar Item在分割到底部时的背景。

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="AppBaseTheme" parent="@style/Theme.AppCompat">
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>

    <style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">
        <item name="background">@drawable/A</item>
        <item name="backgroundStacked">@drawable/B</item>
        <item name="backgroundSplit">@drawableC</item>
    </style>
    
    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme"></style>

</resources>

字体

“android:titleTextStyle”——ActionBar本身的字体。

“android:actionBarTabTextStyle”——ActionBar被分离时Tab的字体。

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="AppBaseTheme" parent="@style/Theme.AppCompat">
        <item name="titleTextStyle">@style/MyActionBarTitleText</item>
        <item name="actionBarTabTextStyle">@style/MyActionBarTabText</item>
    </style>

    <style name="MyActionBarTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">#fff</item>
        <item name="android:textSize">20sp</item>
        <item name="android:textStyle">bold</item>
    </style>

    <style name="MyActionBarTabText" parent="@style/Widget.AppCompat.ActionBar.TabText">
        <item name="android:textColor">#fff</item>
        <item name="android:textSize">20sp</item>
        <item name="android:textStyle">bold</item>
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme"></style>

</resources>

“android:titleTextStyle”,经测试此方法在android 2.x上无效。

附上在android 2.x有效代码。

actionBar = getSupportActionBar();
// 自定义字体
SpannableString spannableString = new SpannableString("TimeToDo");
// 设置字体
spannableString.setSpan(new TypefaceSpan("monospace"), 0,
		spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// 字体大小
spannableString.setSpan(new AbsoluteSizeSpan(40, true), 0,
		spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// 字体颜色
spannableString.setSpan(new ForegroundColorSpan(Color.MAGENTA), 0,
		spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//ActionBar本身的字体
actionBar.setTitle(spannableString);  

Tab Indicator

“android:actionBarTabStyle”——Tab Indicator样式。

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="AppBaseTheme" parent="@style/Theme.AppCompat">
        <item name="actionBarTabStyle">@style/MyActionBarTabs</item>
    </style>

    <style name="MyActionBarTabs" parent="@style/Widget.AppCompat.ActionBar.TabView">
        <item name="android:background">@drawable/actionbar_tab_indicator</item>
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme"></style>

</resources>

 res/drawable/actionbar_tab_indicator.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_selected="false"
          android:state_pressed="false"
          android:drawable="@drawable/tab_unselected" />
    <item android:state_selected="true"
          android:state_pressed="false"
          android:drawable="@drawable/tab_selected" />
    <item android:state_selected="false"
          android:state_pressed="true"
          android:drawable="@drawable/tab_unselected_pressed" />
    <item android:state_selected="true"
          android:state_pressed="true"
          android:drawable="@drawable/tab_selected_pressed" />

</selector>

Spinner

"android:actionDropDownStyle"——下拉框背景(包括选中/按下/j解除状态)。

"android:dropDownListViewStyle"——下拉列表背景(包括选中/按下/解除状态)。

"android:spinnerDropDownItemStyle"——下拉框及列表字体。

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="actionDropDownStyle">@style/MyDropDownNav</item>
        <item name="dropDownListViewStyle">@style/MyDropDownListView</item>
        <item name="spinnerDropDownItemStyle">@style/MyDropDownItemStyle</item>
    </style>

    <style name="MyDropDownNav" parent="@style/Widget.AppCompat.Base.Spinner">
        <item name="android:background">@drawable/mab_spinner_background_holo_dark</item>
        <item name="android:popupBackground">@drawable/abc_menu_dropdown_panel_holo_dark</item>
    </style>

    <style name="MyDropDownListView" parent="@style/Widget.AppCompat.Base.ListView.DropDown">
        <item name="android:listSelector">@drawable/mab_selectable_background</item>
    </style>

    <style name="MyDropDownItemStyle" parent="@style/Widget.AppCompat.Base.DropDownItem.Spinner">
        <item name="android:textAppearance">@style/MyDropDownItemTextStyle</item>
    </style>

    <style name="MyDropDownItemTextStyle" parent="@style/Widget.AppCompat.Spinner.DropDown.ActionBar">
        <item name="android:textColor">#FFFFFF</item>
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme"></style>

</resources>

 mab_selectable_background

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/@drawable/abc_list_pressed_holo_dark" android:state_pressed="true"/>

</selector>

 mab_spinner_background_holo_light.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/abc_spinner_ab_pressed_holo_dark" android:state_pressed="true"/>
    <item android:drawable="@drawable/abc_spinner_ab_disabled_holo_dark" android:state_enabled="false"/>
    <item android:drawable="@drawable/abc_spinner_ab_focused_holo_dark" android:state_focused="true" android:state_pressed="false"/>
    <item android:drawable="@drawable/abc_spinner_ab_default_holo_dark"/>

</selector>

其他

“android:actionBarSize”——ActionBar高度。

——无

“android:actionButtonStyle”——菜单按钮背景。

——@style/Widget.AppCompat.ActionButton

“android:actionOverflowButtonStyle”——Overflow背景。

——@style/Widget.AppCompat.ActionButton.Overflow

“android:selectableItemBackground”——Home按钮背景。

——无

“android:actionBarDivider”——分隔线背景。

——无

“android:actionMenuTextAppearance”——菜单按钮文本样式。

——无

“android:actionMenuTextColor”——菜单按钮文本颜色。

——无

已知不能更改的属性:二级菜单、ActionBar标题(可通过代码实现)。

已知问题:在Android 4.x会二次加载界面(第一次为原始ActionBar样式)。

猜你喜欢

转载自irtutsk.iteye.com/blog/2118169