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

我已经能够自定义动作栏的背景,标志图片和文字颜色使用这些建议: < br > Android: 如何改变动作栏的“主页”图标为应用程序图标以外的东西? < br > ActionBar 文本颜色 < br > 动作条背景图片

我想定制的最后一件是后退按钮图像。它默认是灰色的,我希望它是白色的。要么改变颜色,指定一个可绘制的或者只是使其透明(并添加到我的自定义徽标图像的人字符)将工作。我该怎么做?

197298 次浏览

主题的 homeAsUpIndicator属性中指定的可绘制图形提供了“向上”可见性指示器。要用自定义版本覆盖它,应该是这样的:

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

如果您的应用程序支持 pre-3.0,请确保将这个版本的自定义主题放在 values-v11或类似的版本中。

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

覆盖前 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 或更高 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>

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

问题在于,对于 API 11或更高的设备,Sherlock Action Bar 使用 Android 自带的默认 Action Bar,其关键名是 Android: homeAsUpIndicator。但对于 API 10或更低的设备,Sherlock Action Bar 使用自己的 ActionBar,其主页作为向上指示符被称为简单的“ homeAsUpIndicator”。

在清单中使用新主题

替换 AndroidManifest 文件中的 application/activity 的主题:

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

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

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

如果使用支持库

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

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

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


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

如果你使用的是 dark. actionBar,那么你的后退按钮就是白色的; 如果你使用的是 light actionbar 主题,那么后退按钮就是黑色的。

我做了下面的代码 onCreate()和我一起工作

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

更改 ActionBar 和 Toolbar 的返回导航图标是不同的。

对于 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>

我在 menifest.xml 文件中使用了 back.png 图像。

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

我有同样的问题,行动栏首页按钮图标的方向,因为错过了管理的图标在格拉德资源目录图标

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

托盘:

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

onCreate()内;