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

发布于 2025-01-05 09:20:11 字数 515 浏览 1 评论 0原文

我已经能够使用以下建议自定义操作栏的背景、徽标图像和文本颜色:
Android:如何将 ActionBar“主页”图标更改为应用程序图标以外的其他图标?
操作栏文本颜色
ActionBar 背景图像

我要自定义的最后一块是后退按钮图像。默认情况下它是灰色的,我希望它是白色的。更改颜色、指定可绘制对象或简单地使其透明(并将 V 形添加到我的自定义徽标图像中)都可以。我该怎么办?

I have been able to customize the action bar's background, logo image and text color using suggestions from these:
Android: How to change the ActionBar "Home" Icon to be something other than the app icon?
ActionBar text color
ActionBar background image

The last piece I want to customize is the back button image. It's grey by default and I would like it to be white. Either changing the color, specifying a drawable or simply making it transparent (and adding the chevron to my customized logo image) would work. How do I go about that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

听,心雨的声音 2025-01-12 09:20:11

“向上”可供性指示器由主题的 homeAsUpIndicator 属性中指定的可绘制对象提供。要使用您自己的自定义版本覆盖它,如下所示:

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

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

The "up" affordance indicator is provided by a drawable specified in the homeAsUpIndicator attribute of the theme. To override it with your own custom version it would be something like this:

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

If you are supporting pre-3.0 with your application be sure you put this version of the custom theme in values-v11 or similar.

无尽的现实 2025-01-12 09:20:11

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

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

如果您使用支持库

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

So you can change it programmatically easily by using homeAsUpIndicator() function that added in android API level 18 and upper.

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

If you use support library

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

婴鹅 2025-01-12 09:20:11

我已经检查过这个问题。这是我遵循的步骤。源代码托管在 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+ 设备的实际样式。

在文件夹 value 所在的同一文件夹中,创建一个名为 value-v11 的新文件夹。对于具有API或以上版本的设备,Android会自动查找该文件夹的内容。

创建一个名为 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自带的默认Action Bar,其键名是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" >

I have checked the question. Here is the steps that I follow. The source code is hosted on GitHub:
https://github.com/jiahaoliuliu/sherlockActionBarLab

Override the actual style for the pre-v11 devices.

Copy and paste the follow code in the file styles.xml of the default values folder.

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

Note that the parent could be changed to any Sherlock theme.

Override the actual style for the v11+ devices.

On the same folder where the folder values is, create a new folder called values-v11. Android will automatically look for the content of this folder for devices with API or above.

Create a new file called styles.xml and paste the follow code into the file:

<?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>

Note tha the name of the style must be the same as the file in the default values folder and instead of the item homeAsUpIndicator, it is called android:homeAsUpIndicator.

The item issue is because for devices with API 11 or above, Sherlock Action Bar use the default Action Bar which comes with Android, which the key name is android:homeAsUpIndicator. But for the devices with API 10 or lower, Sherlock Action Bar uses its own ActionBar, which the home as up indicator is called simple "homeAsUpIndicator".

Use the new theme in the manifest

Replace the theme for the application/activity in the AndroidManifest file:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/MyCustomTheme" >
意犹 2025-01-12 09:20:11

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>

Changing back navigation icon differs for ActionBar and Toolbar.

For ActionBar override homeAsUpIndicator attribute:

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

For Toolbar override navigationIcon attribute:

<style name="CustomThemeToolbar" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="navigationIcon">@drawable/ic_nav_back</item>
</style>
懒猫 2025-01-12 09:20:11

我执行了以下代码 onCreate() 并与我一起工作

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

I did the below code onCreate() and worked with me

getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_yourindicator);
夜深人未静 2025-01-12 09:20:11

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

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

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

如果您使用的是 dark.actionBar,您的后退按钮将是白色的,否则如果您使用的是浅色操作栏主题,它将是黑色的。

If you are using Toolbar, you don't need those solutions. You only have to change the theme of the toolbar

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

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

If you are using a dark.actionBar your back button is going to be white else if you are using light actionbar theme it is going to be black.

初吻给了烟 2025-01-12 09:20:11

托盘:

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

onCreate()内;

tray this:

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

inside onCreate();

沦落红尘 2025-01-12 09:20:11

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

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

I used back.png image in the project menifest.xml file. it is fine working in project.

<activity
        android:name=".YourActivity"
         android:icon="@drawable/back"
        android:label="@string/app_name" >
    </activity>
趁年轻赶紧闹 2025-01-12 09:20:11

我遇到了与操作栏主页按钮图标方向相同的问题,因为缺少对 Gradle 资源目录图标中图标的管理,

例如在阿拉伯语 Gradle 资源目录中,您将图标放入 x-hdpi 中,而在英语 Gradle 资源中,您将相同的图标名称放入不同的图标中密度文件夹,如 xx-hdpi,这样在 APK 中,不同的目录中会有两个相同的图标名称,因此您的设备将选择密度相关的图标,可能是 RTL 或 LTR

I had the same issue of Action-bar Home button icon direction,because of miss managing of Icon in Gradle Resource directory icon

like in Arabic Gradle resource directory you put icon in x-hdpi and in English Gradle resource same icon name you put in different density folder like xx-hdpi ,so that in APK there will be two same icon names in different directories,so your device will pick density dependent icon may be RTL or LTR

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文