检测选项菜单的背景颜色
我发现Android的选项菜单上至少有两种背景颜色。在 HTC Hero 上,背景为白色,在 Samsung Galaxy S II 上,背景为黑色。
当我设置背景菜单的图标时,这成为一个问题。有没有办法检测Android中选项菜单的背景颜色?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
可能的解决方案:
对于 Android 4.0+,您还可以设置浅色/深色 Holo 主题,保证(至少理论上)在不同手机制造商之间保持不变 - 因此它在 HTC Sense、三星 TouchWiz 中看起来都是一样的 ETC。
Possible solutions:
For Android 4.0+, you can also set the light / dark Holo theme, which is guaranteed (at least in theory) to remain unchanged across different phone manufacturers - so it'll look the same in HTC Sense, Samsung TouchWiz etc.
这确实是一个非常烦人的问题。
在我的实现中,我总是尝试使用 android.R.drawable.IC_menu_* 中的标准图标,因此我确信这些图标是框架的一部分,并且用户总是会惊讶地看到它们的通用图标在我的应用程序中!
这为设备上的用户体验提供了非常好的连续性,但这并不能回答您的问题,但至少提供了一种解决方法。
例如,以下是所有 android 2.2 图标: http://androiddrawableexplorer.appspot.com/
您可以相信我,使用这些图标将始终适合您的颜色。
That's indeed a very annoying issue.
On my implementation, I always try to use standards icons from android.R.drawable.IC_menu_*, so I am sure these icons are part of the framework and users are always positively surprised to see their generic icons in my app!
That gives a really good continuity in the user experience on the device, but that doesn't answer your question, but at least provide a workaround.
Here are for instance all android 2.2 icons: http://androiddrawableexplorer.appspot.com/
You can trust me, using these icons will always fit your colors.
首先回答您的标题问题:
您可以通过阅读当前主题的属性。其属性是panelFullBackground。例如,将其设置为 XML 中文本视图的背景:
请注意,背景似乎是纯色,但事实并非如此 - 它实际上是一个位图。如果你仔细观察,你可以看到顶部有一个灰色边框(android 2.3+),或者一个阴影(<= android 2.2),所以它有点困难。幸运的是,有一个名为
panelColorBackground
的不同属性,它尽可能接近地匹配可绘制对象的背景颜色。因此,如果您只想要正常的背景颜色,请使用此颜色。¹ 这当然也可以从代码中读取,但我现在不知道如何从我的头脑中读取它,也许我会查找它并稍后编辑它。
关于图标
所以你有了上面所说的颜色,但是你仍然需要处理它来知道它是深色还是亮色。您可以这样做,但这不是处理这些图标的常用方法,并且在您涵盖所有可能的情况之前可能需要大量工作 - 更不用说您必须为每个变体创建图标。
通常应该采用平台菜单图标样式。这适用于所有设备,并且对您的用户来说看起来很熟悉(不遵循这一点的自定义图标通常看起来“错误” - 例如,我相信 astro 文件管理器会这样做)。
您可以手动完成此操作(请参阅指南)< /em>,但更好的选择是 Android Asset Studio。
它有两种风格:
(在
文件->新建->其他->Android图标集
下)两者的工作流程非常相似,选择点“菜单图标”并按照向导。它将提示您输入所需图标的简单黑白位图,该图标仅勾勒出其形状。在您指定后,资产工作室将为您生成所有内容。尝试一下“clipart”选项,其中有一些示例位图可供查看其工作原理。完成后,Web 应用程序会为您提供一个简单的 zip 文件,可以将其提取到您的项目目录中,而 Eclipse 版本则将其直接添加到您在向导中选择的项目中。
First of all to answer your title question:
You can reference and read the background of the options menu by reading the attributes of the current theme. The attribute for this is
panelFullBackground
. E.g. set it as the background of a textview in XML¹:Note that the background seems to be a solid color, but it's not - it's actually a bitmap. If you look closely you can see a grey border at the top (android 2.3+), or a drop shadow (<= android 2.2), so its'a bit difficult. Luckily there is a different attribute called
panelColorBackground
which matches the background color of the drawable as close as possible. So if you want just the normal background color, use this instead.¹ This can surely also be read from code, but I don't know how from the top of my head at the moment, maybe I'll look it up and edit it in later.
Regarding icons
So you have the color as stated above, but you still have to process it to know if it's a dark or a bright color. You can do that, but that's not the usual way to deal with these icons and probably a good bit of work until you cover all the possible cases - not to mention that you have to create icons for each variant.
Normally you should adopt the platform menu icon style. This works across all devices and looks familiar to your users (custom icons that dont follow this often look "wrong" - e.g. astro file manager does this I believe).
You can do that by hand (see the guidelines), but the way better alternative is the Android Asset Studio.
It comes in two flavors:
(under
File->New->Other->Android Icon Set
)The workflow for both is pretty similar, select the point "Menu Icon" and follow the wizard. It will promt you to enter a simple, black and white bitmap of your desired icon that just outlines it's shape. After you specified one, the asset studio will generate everything for you. Play a bit around with the "clipart" option, that has a few example bitmaps ready to see how it works. When finished, the webapp gives you a simple zip which can be extracted into your project directory, the eclipse version adds it directly to the project that you select in the wizard.
背景颜色可以是任何颜色,因为它是由制造商在框架中实现的。你无法阅读它,事实上你永远不需要阅读它。
只需在 res/menu 文件夹中创建自定义菜单布局,设置样式并使用它。
The background color can be anything, because its implemented in Framework by manufacturer. You can't read it, in fact you will never need to read it.
Just create your custom menu layout in res/menu folder, set style and use it.