简体   繁体   English

如何设置actionBar中显示的菜单项的背景

[英]how to set background for menu item shown in actionBar

I am showing a menu item in the action bar. 我在操作栏中显示一个菜单项。 I would like to set a background for this item. 我想为这个项目设置背景。 I would prefer not to use an icon because otherwise I cannot use strings.xml for localization (I am showing some text in this particular menu item, that will get translated in several languages.). 我宁愿不使用图标,因为否则我不能使用strings.xml进行本地化(我在这个特定的菜单项中显示一些文本,将以多种语言翻译)。

Most of answers here suggest to use icons. 这里的大多数答案建议使用图标。 The icon of course would cover the text, and if I try android:background instead of android:icon it just won't show anything. 图标当然会覆盖文本,如果我尝试android:background而不是android:icon它就不会显示任何内容。

So basically I want to customize the menu item in the actionbar with a custom image but at the same time be able to write something on it (pull some strings). 所以基本上我想用自定义图像自定义操作栏中的菜单项,但同时能够在其上写一些东西(拉一些字符串)。 Is there a way to this in xml? 在xml中有这种方法吗?

PS I am using Theme.Holo.Light.DarkActionBar but I already changed the background of the actionbar. PS我正在使用Theme.Holo.Light.DarkActionBar,但我已经改变了操作栏的背景。

As a workaround you could always have different icons for your localization. 作为一种解决方法,您可以始终为本地化提供不同的图标。 There is nothing stopping you having: 没有什么可以阻止你:

/drawable-fr/
/drawable-us/

On the other hand if you believe it is something to do with the Theme you are inheriting you could look through the source code for the DarkActionBar and then extend this in styles.xml overriding the values you want to change (the color of). 另一方面,如果您认为它与您继承的主题有关,您可以查看DarkActionBar的源代码,然后在styles.xml中扩展它,覆盖您想要更改的值(颜色)。

https://github.com/android/platform_frameworks_base/tree/master/core/res/res/values https://github.com/android/platform_frameworks_base/tree/master/core/res/res/values

Create an xml file per each icon you wan to show in the action bar in the 'drawable' directory like the one below: 为每个要在“drawable”目录的操作栏中显示的图标创建一个xml文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/menuitem_bk"/>
    <item android:drawable="@drawable/icon_one"/>      
</layer-list>

Drawable icon_one is just the icon with a transparent background and 'menuitem_bk' will be a drawable defined as follows: Drawable icon_one只是带有透明背景的图标,'menuitem_bk'将是一个drawable,定义如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <size
        android:height="36dp"
        android:width="36dp" />
    <solid android:color="@color/action_bar_items_bk_color" />
</shape>

You will have to create four of these, one in each drawable directory (xhdpi, hdpi, mdpi and ldpi). 您将必须创建其中的四个,每个可绘制目录中有一个(xhdpi,hdpi,mdpi和ldpi)。 The size of the shape will be different according to the directory: 根据目录,形状的大小会有所不同:

  • xhdpi -> 48x48 hdpi -> 36x36 mdpi -> 24x24 ldpi -> 18x18 xhdpi - > 48x48 hdpi - > 36x36 mdpi - > 24x24 ldpi - > 18x18

(Acording to this: http://developer.android.com/guide/practices/ui_guidelines/icon_design_action_bar.html ) (根据这一点: http//developer.android.com/guide/practices/ui_guidelines/icon_design_action_bar.html

And you got your ActionBar icons with background. 你得到了带有背景的ActionBar图标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM