如何自定义ActionBar上的后退按钮
我已经能够使用以下建议自定义操作栏的背景、徽标图像和文本颜色:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
“向上”可供性指示器由主题的 homeAsUpIndicator 属性中指定的可绘制对象提供。要使用您自己的自定义版本覆盖它,如下所示:
如果您的应用程序支持 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: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.因此,您可以使用 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);
我已经检查过这个问题。这是我遵循的步骤。源代码托管在 GitHub 上:
https://github.com/jiahaoliuliu/sherlockActionBarLab
覆盖 v11 之前设备的实际样式。
将以下代码复制并粘贴到默认值文件夹的 styles.xml 文件中。
请注意,父主题可以更改为任何 Sherlock 主题。
覆盖 v11+ 设备的实际样式。
在文件夹 value 所在的同一文件夹中,创建一个名为 value-v11 的新文件夹。对于具有API或以上版本的设备,Android会自动查找该文件夹的内容。
创建一个名为 styles.xml 的新文件,并将以下代码粘贴到该文件中:
请注意,样式的名称必须与默认值文件夹中的文件相同,并且名称不是 homeAsUpIndicator,而是 android:homeAsUpIndicator。
该问题是因为对于API 11或以上的设备,Sherlock Action Bar使用Android自带的默认Action Bar,其键名是android:homeAsUpIndicator。但对于 API 10 或更低版本的设备,Sherlock Action Bar 使用自己的 ActionBar,其中 home 作为向上指示器,称为简单的“homeAsUpIndicator”。
在清单中使用新主题
替换 AndroidManifest 文件中应用程序/活动的主题:
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.
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:
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:
ActionBar 和 Toolbar 的返回导航图标的更改有所不同。
对于 ActionBar 覆盖
homeAsUpIndicator
属性:对于工具栏覆盖
navigationIcon
属性:Changing back navigation icon differs for ActionBar and Toolbar.
For ActionBar override
homeAsUpIndicator
attribute:For Toolbar override
navigationIcon
attribute:我执行了以下代码
onCreate()
并与我一起工作I did the below code
onCreate()
and worked with me如果您使用工具栏,则不需要这些解决方案。您只需要更改工具栏的主题
如果您使用的是 dark.actionBar,您的后退按钮将是白色的,否则如果您使用的是浅色操作栏主题,它将是黑色的。
If you are using Toolbar, you don't need those solutions. You only have to change the theme of the toolbar
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.
托盘:
在
onCreate()
内;tray this:
inside
onCreate()
;我在项目 menifest.xml 文件中使用了 back.png 图像。在项目中工作很好。
I used back.png image in the project menifest.xml file. it is fine working in project.
我遇到了与操作栏主页按钮图标方向相同的问题,因为缺少对 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