在android中自定义按钮

时间:2013-08-22 06:33:22

标签: android android-actionbar-compat

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

enter image description here

我为它准备了自己的形象。所以我想用我的图像替换它。我相信这是可能的。因为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>

3 个答案:

答案 0 :(得分:6)

将此项添加到与预蜂窝相关的自定义主题中会出现错误,因为旧版设备上的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>

这种方式应该能够在您的活动中看到this

答案 1 :(得分:0)

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

android:logo

因为ActionBar将使用清单的android:logo属性,如果提供了一个。这使您可以为图标(Launcher)和徽标(ActionBar等)使用单独的可绘制资源。

答案 2 :(得分:0)

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

您需要将以下项添加到Application主题XML。

<item name="android:homeAsUpIndicator">@drawable/action_up_caret</item>
相关问题