首页 文章

如何自定义ActionBar上的后退按钮

提问于
浏览
185

我可以使用以下建议自定义操作栏的背景,徽标图像和文本颜色:
Android: How to change the ActionBar "Home" Icon to be something other than the app icon?
ActionBar text color
ActionBar background image

我要自定义的最后一块是后退按钮图像 . 它默认为灰色,我希望它是白色的 . 更改颜色,指定可绘制或简单地使其透明(并将V形符号添加到我的自定义徽标图像)将起作用 . 我该怎么做?

8 回答

  • 380

    "up"可供性指标由主题的 homeAsUpIndicator 属性中指定的drawable提供 . 要使用您自己的自定义版本覆盖它,它将是这样的:

    <style name="Theme.MyFancyTheme" parent="android:Theme.Holo">
        <item name="android:homeAsUpIndicator">@drawable/my_fancy_up_indicator</item>
    </style>
    

    如果您使用您的应用程序支持3.0之前的版本,请确保将此版本的自定义主题放在 values-v11 或类似内容中 .

  • 0

    因此,您可以使用在android API级别18和更高版本中添加的homeAsUpIndicator()函数轻松地以编程方式更改它 .

    ActionBar().setHomeAsUpIndicator(R.drawable.ic_yourindicator);

    如果您使用支持库

    getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_yourindicator);

  • 5

    我已经检查了这个问题 . 以下是我遵循的步骤 . 源代码托管在GitHub上:https://github.com/jiahaoliuliu/sherlockActionBarLab

    Override the actual style for the pre-v11 devices.

    将以下代码复制并粘贴到默认值文件夹的文件styles.xml中 .

    <resources>
        <style name="MyCustomTheme" parent="Theme.Sherlock.Light">
        <item name="homeAsUpIndicator">@drawable/ic_home_up</item>
        </style>
    </resources>
    

    请注意,父级可以更改为任何Sherlock主题 .

    Override the actual style for the v11+ devices.

    在文件夹值所在的同一文件夹中,创建一个名为values-v11的新文件夹 . Android会自动为API或更高版本的设备查找此文件夹的内容 .

    创建一个名为styles.xml的新文件,并将以下代码粘贴到文件中:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="MyCustomTheme" parent="Theme.Sherlock.Light">
        <item name="android:homeAsUpIndicator">@drawable/ic_home_up</item>
        </style>
    </resources>
    

    请注意,样式的名称必须与默认值文件夹中的文件相同,而不是项目homeAsUpIndicator,它名为android:homeAsUpIndicator .

    项目问题是因为对于具有API 11或更高版本的设备,Sherlock Action Bar使用Android附带的默认操作栏,其键名为android:homeAsUpIndicator . 但对于API 10或更低版本的设备,Sherlock Action Bar使用自己的ActionBar,其中home作为向上指示器称为简单的“homeAsUpIndicator” .

    Use the new theme in the manifest

    替换AndroidManifest文件中的应用程序/活动的主题:

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/MyCustomTheme" >
    
  • 24

    更改后退导航图标因ActionBar和工具栏而异 .

    对于ActionBar覆盖 homeAsUpIndicator 属性:

    <style name="CustomThemeActionBar" parent="android:Theme.Holo">
        <item name="homeAsUpIndicator">@drawable/ic_nav_back</item>
    </style>
    

    对于工具栏覆盖 navigationIcon 属性:

    <style name="CustomThemeToolbar" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="navigationIcon">@drawable/ic_nav_back</item>
    </style>
    
  • 83

    我做了以下代码 onCreate() 并与我合作

    getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_yourindicator);
    
  • 19

    如果您使用的是工具栏,则不需要这些解决方案 . 您只需更改工具栏的主题即可

    app:theme="@style/ThemeOverlay.AppCompat.Light"
    
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    

    如果你使用的是dark.actionBar你的后退按钮将是白色的,如果你使用轻型动作条主题,它将是黑色的 .

  • 0

    我在项目menifest.xml文件中使用了back.png图像 . 在项目中工作很好 .

    <activity
            android:name=".YourActivity"
             android:icon="@drawable/back"
            android:label="@string/app_name" >
        </activity>
    
  • 30

    由于错过了Gradle资源目录图标中Icon的管理,我遇到了Action-bar Home按钮图标方向相同的问题

    就像在阿拉伯语Gradle资源目录中一样,你把图标放在x-hdpi和英文Gradle资源中相同的图标名称你放在不同的密度文件夹中,比如xx-hdpi,这样在APK中会有两个相同的图标名称在不同的目录中,所以你的设备将选择密度相关的图标可以是RTL或LTR

相关问题