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

问题描述 投票:185回答:9

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

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

android android-actionbar
9个回答
383
投票

“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或类似版本中。


85
投票

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

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

如果您使用支持库

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


30
投票

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

覆盖pre-v11设备的实际样式。

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

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

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

覆盖v11 +设备的实际样式。

在文件夹值所在的同一文件夹中,创建一个名为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”。

在清单中使用新主题

替换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>

19
投票

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

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

5
投票

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

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

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

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


1
投票

托盘这个:

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

onCreate()内;


0
投票

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

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

0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.