2

我想自定义up button,我正在使用appcompat library我的操作栏。我不想要android提供的默认向上按钮。因为我使用actionbar的是style generator 产生的。所以这个向上按钮看起来很苍白actionbar,我选择的颜色。有人可以建议我如何实现吗?我搜索了很多,但找不到。

在此处输入图像描述

我已经为它准备了我自己的形象。所以我想用我的图像替换它。我相信这是可能的。因为 googleplay 在应用程序部分以不同颜色显示带有向上按钮的操作栏。

下面是styles.xml:

     <style name="Theme.Customab" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="actionBarItemBackground">@drawable/selectable_background_customab</item>
        <item name="popupMenuStyle">@style/PopupMenu.Customab</item>
        <item name="dropDownListViewStyle">@style/DropDownListView.Customab</item>
        <item name="actionBarTabStyle">@style/ActionBarTabStyle.Customab</item>
        <item name="actionDropDownStyle">@style/DropDownNav.Customab</item>
        <item name="actionBarStyle">@style/ActionBar.Solid.Customab</item>
        <item name="actionModeBackground">@drawable/cab_background_top_customab</item>
        <item name="actionModeSplitBackground">@drawable/cab_background_bottom_customab</item>
        <item name="actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Customab</item>

                <!-- Light.DarkActionBar specific -->
        <item name="actionBarWidgetTheme">@style/Theme.Customab.Widget</item>

**<item name="android:homeAsUpIndicator">@drawable/customup</item>**


    </style>

    <style name="ActionBar.Solid.Customab" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="background">@drawable/ab_solid_customab</item>
        <item name="backgroundStacked">@drawable/ab_stacked_solid_customab</item>
        <item name="backgroundSplit">@drawable/ab_bottom_solid_customab</item>
        <item name="progressBarStyle">@style/ProgressBar.Customab</item>
    </style>

    <style name="ActionBar.Transparent.Customab" parent="@style/Widget.AppCompat.ActionBar">
        <item name="background">@drawable/ab_transparent_customab</item>
        <item name="progressBarStyle">@style/ProgressBar.Customab</item>
    </style>

    <style name="PopupMenu.Customab" parent="@style/Widget.AppCompat.PopupMenu">    
        <item name="android:popupBackground">@drawable/menu_dropdown_panel_customab</item>  
    </style>

    <style name="DropDownListView.Customab" parent="@style/Widget.AppCompat.ListView.DropDown">
        <item name="android:listSelector">@drawable/selectable_background_customab</item>
    </style>

    <style name="ActionBarTabStyle.Customab" parent="@style/Widget.AppCompat.ActionBar.TabView">
        <item name="android:background">@drawable/tab_indicator_ab_customab</item>
    </style>

    <style name="DropDownNav.Customab" parent="@style/Widget.AppCompat.Spinner.DropDown.ActionBar">
        <item name="android:background">@drawable/spinner_background_ab_customab</item>
        <item name="android:popupBackground">@drawable/menu_dropdown_panel_customab</item>
        <item name="android:dropDownSelector">@drawable/selectable_background_customab</item>
    </style>

    <style name="ProgressBar.Customab" parent="@style/Widget.AppCompat.ProgressBar.Horizontal">
        <item name="android:progressDrawable">@drawable/progress_horizontal_customab</item>
    </style>

    <style name="ActionButton.CloseMode.Customab" parent="@style/Widget.AppCompat.ActionButton.CloseMode">
        <item name="android:background">@drawable/btn_cab_done_customab</item>
    </style>

    <!-- this style is only referenced in a Light.DarkActionBar based theme -->
    <style name="Theme.Customab.Widget" parent="@style/Theme.AppCompat">
        <item name="popupMenuStyle">@style/PopupMenu.Customab</item>
        <item name="dropDownListViewStyle">@style/DropDownListView.Customab</item>
    </style>
4

3 回答 3

6

将此项目添加到与 pre-Honeycomb 相关的自定义主题会产生错误,因为旧设备上的 Android 框架中不存在该属性。

<item name="android:homeAsUpIndicator">@drawable/action_up_caret</item>

如果您使用 AppCompat 库,则应在自定义主题中覆盖“homeAsUpIndicator”属性,而不是“android:homeAsUpIndicator”。

<item name="homeAsUpIndicator">@drawable/action_up_caret</item>

注意:如果您有兴趣,这里是 AppCompat 源代码。https://github.com/android/platform_frameworks_support/tree/master/v7/appcompathttps://android.googlesource.com/platform/frameworks/support/+/master/v7/appcompat/

这是我的测试项目的一些文件:

值\styles_example.xml:

<?xml version="1.0" encoding="utf-8"?>
    <resources>

    <style name="Theme.Example" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarItemBackground">@drawable/selectable_background_example</item>
        <item name="popupMenuStyle">@style/PopupMenu.Example</item>
        <item name="dropDownListViewStyle">@style/DropDownListView.Example</item>
        <item name="actionBarTabStyle">@style/ActionBarTabStyle.Example</item>
        <item name="actionDropDownStyle">@style/DropDownNav.Example</item>
        <item name="actionBarStyle">@style/ActionBar.Solid.Example</item>
        <item name="actionModeBackground">@drawable/cab_background_top_example</item>
        <item name="actionModeSplitBackground">@drawable/cab_background_bottom_example</item>
        <item name="actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Example</item>

                <!-- Light.DarkActionBar specific -->
        <item name="actionBarWidgetTheme">@style/Theme.Example.Widget</item>

        <item name="homeAsUpIndicator">@drawable/ic_launcher</item>
    </style>

    <style name="ActionBar.Solid.Example" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="background">@drawable/ab_solid_example</item>
        <item name="backgroundStacked">@drawable/ab_stacked_solid_example</item>
        <item name="backgroundSplit">@drawable/ab_bottom_solid_example</item>
        <item name="progressBarStyle">@style/ProgressBar.Example</item>
    </style>

    <style name="ActionBar.Transparent.Example" parent="@style/Widget.AppCompat.ActionBar">
        <item name="background">@drawable/ab_transparent_example</item>
        <item name="progressBarStyle">@style/ProgressBar.Example</item>
    </style>

    <style name="PopupMenu.Example" parent="@style/Widget.AppCompat.PopupMenu"> 
        <item name="android:popupBackground">@drawable/menu_dropdown_panel_example</item>   
    </style>

    <style name="DropDownListView.Example" parent="@style/Widget.AppCompat.ListView.DropDown">
        <item name="android:listSelector">@drawable/selectable_background_example</item>
    </style>

    <style name="ActionBarTabStyle.Example" parent="@style/Widget.AppCompat.ActionBar.TabView">
        <item name="android:background">@drawable/tab_indicator_ab_example</item>
    </style>

    <style name="DropDownNav.Example" parent="@style/Widget.AppCompat.Spinner.DropDown.ActionBar">
        <item name="android:background">@drawable/spinner_background_ab_example</item>
        <item name="android:popupBackground">@drawable/menu_dropdown_panel_example</item>
        <item name="android:dropDownSelector">@drawable/selectable_background_example</item>
    </style>

    <style name="ProgressBar.Example" parent="@style/Widget.AppCompat.ProgressBar.Horizontal">
        <item name="android:progressDrawable">@drawable/progress_horizontal_example</item>
    </style>

    <style name="ActionButton.CloseMode.Example" parent="@style/Widget.AppCompat.ActionButton.CloseMode">
        <item name="android:background">@drawable/btn_cab_done_example</item>
    </style>

    <!-- this style is only referenced in a Light.DarkActionBar based theme -->
    <style name="Theme.Example.Widget" parent="@style/Theme.AppCompat">
        <item name="popupMenuStyle">@style/PopupMenu.Example</item>
        <item name="dropDownListViewStyle">@style/DropDownListView.Example</item>
    </style>

</resources>

值-v14\styles_example.xml:

<?xml version="1.0" encoding="utf-8"?>
    <resources>

    <style name="Theme.Example" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarItemBackground">@drawable/selectable_background_example</item>
        <item name="android:popupMenuStyle">@style/PopupMenu.Example</item>
        <item name="android:dropDownListViewStyle">@style/DropDownListView.Example</item>
        <item name="android:actionBarTabStyle">@style/ActionBarTabStyle.Example</item>
        <item name="android:actionDropDownStyle">@style/DropDownNav.Example</item>
        <item name="android:actionBarStyle">@style/ActionBar.Solid.Example</item>
        <item name="android:actionModeBackground">@drawable/cab_background_top_example</item>
        <item name="android:actionModeSplitBackground">@drawable/cab_background_bottom_example</item>
        <item name="android:actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Example</item>

                <!-- Light.DarkActionBar specific -->
        <item name="android:actionBarWidgetTheme">@style/Theme.Example.Widget</item>

        <item name="android:homeAsUpIndicator">@drawable/ic_launcher</item>
    </style>

    <style name="ActionBar.Solid.Example" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@drawable/ab_solid_example</item>
        <item name="android:backgroundStacked">@drawable/ab_stacked_solid_example</item>
        <item name="android:backgroundSplit">@drawable/ab_bottom_solid_example</item>
        <item name="android:progressBarStyle">@style/ProgressBar.Example</item>
    </style>

    <style name="ActionBar.Transparent.Example" parent="@style/Widget.AppCompat.ActionBar">
        <item name="android:background">@drawable/ab_transparent_example</item>
        <item name="android:progressBarStyle">@style/ProgressBar.Example</item>
    </style>

    <!-- this style is only referenced in a Light.DarkActionBar based theme -->
    <style name="Theme.Example.Widget" parent="@style/Theme.AppCompat">
        <item name="android:popupMenuStyle">@style/PopupMenu.Example</item>
        <item name="android:dropDownListViewStyle">@style/DropDownListView.Example</item>
    </style>

</resources>

这种方式应该能够在您的活动中得到这样的外观。

于 2013-08-22T08:32:49.857 回答
0

将您的图像保存在可绘制文件夹中指定属性。

android:logo

因为 ActionBar 将使用清单的 android:logo 属性(如果提供的话)。这使您可以为图标(启动器)和徽标(ActionBar 等)使用单独的可绘制资源。

于 2013-08-22T06:59:33.550 回答
0

我没用过AppCompat,但在 Android 4.0 上它很简单(但很难找到)。

您需要将以下项目添加到您的应用程序主题 XML。

<item name="android:homeAsUpIndicator">@drawable/action_up_caret</item>
于 2013-08-22T07:00:37.243 回答