修正:“Android:检测焦点/按下的颜色”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2065430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:23:36  来源:igfitidea点击:

Fixed: "Android: Detecting focus/pressed color"

android

提问by Uro? Milivojevi?

I'm trying to detect the focus/pressed color for button and other elements. This is needed because I'm developing new components and it's important that those look as part of platform. Those colors are ORANGE on android sdk and GREEN on HTC SenseUI. If I could detect that color my component will look as part of platform on both version.

我正在尝试检测按钮和其他元素的焦点/按下颜色。这是必需的,因为我正在开发新组件,重要的是这些组件看起来是平台的一部分。这些颜色在 android sdk 上是橙色,在 HTC SenseUI 上是绿色。如果我能检测到那种颜色,我的组件将在两个版本上看起来都是平台的一部分。

Does anyone knows how to do this?

有谁知道如何做到这一点?



It's possible to create "selector" which uses custom image for default state and platform default for focus/selection.

可以创建“选择器”,它使用自定义图像作为默认状态和平台默认焦点/选择。

To do this follow the steps:1) create xml file with selector in "res/drawable" (e.g. "red_button.xml"):

要做到这一点,请按照以下步骤操作:1)在“res/drawable”(例如“red_button.xml”)中创建带有选择器的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true"
        android:drawable="@android:drawable/btn_default" >
    </item>

    <item android:state_focused="true"
        android:drawable="@android:drawable/btn_default" >
    </item>

    <item 
        android:drawable="@drawable/btn_default_red" >
    </item>
</selector>

2) from folder ".../android-sdk-mac/platforms/android-1.5/data/res/drawable/" take picture "btn_default_pressed.9.png" and change color as you like (I needed to change it to red and for this GIMP is enough).

2)从文件夹“.../android-sdk-mac/platforms/android-1.5/data/res/drawable/”拍摄图片“btn_default_pressed.9.png”并根据需要更改颜色(我需要将其更改为红色,对于这个 GIMP 就足够了)。

3) place altered picture in "res/drawable" (e.g. with name "btn_default_red.9.png")

3) 将修改后的图片放在“res/drawable”中(例如名称为“btn_default_red.9.png”)

4) define button:

4)定义按钮:

<Button
    android:id="@+id/info_button"
    android:layout_width="wrap_content" 
    android:layout_height="37dip"
    android:layout_marginTop="1dip"
    android:background="@drawable/red_button"
    android:text="[Info]" />

That's all.

就这样。

This is result: alt text http://img200.imageshack.us/img200/1349/custombutton.png

这是结果: 替代文字 http://img200.imageshack.us/img200/1349/custombutton.png

回答by Rich

I had this problem too. As already stated, the problem is that the backgrounds aren't simple colors, they're Drawables that could take on all kinds of appearances. However, I found a work-around that may help. If your custom component looks something like an existing one, e.g. a Button or ListView entry, you can just steal their background/selector and set that as the background for your custom component. E.g., in your custom component constructor:

我也有这个问题。如前所述,问题在于背景不是简单的颜色,它们是可以呈现各种外观的 Drawable。但是,我找到了一个可能有帮助的解决方法。如果您的自定义组件看起来像现有组件,例如 Button 或 ListView 条目,您可以窃取它们的背景/选择器并将其设置为您的自定义组件的背景。例如,在您的自定义组件构造函数中:

  setBackgroundDrawable(new Button(context).getBackground());

or for a background more suitable for list-like components:

或者对于更适合类似列表的组件的背景:

  setBackgroundDrawable(new ListView(context).getSelector());

You may want to optimise that code somewhat, but you get the idea.

您可能想稍微优化该代码,但您明白了。

回答by CommonsWare

Those aren't colors. They are a few nine-patch images out of a StateListDrawable. I am skeptical that there will be a reliable way for you to determine what the color is, one that will work across all devices and all versions of Android.

那些不是颜色。它们是一个StateListDrawable. 我怀疑是否会有一种可靠的方法来确定颜色是什么,该方法适用于所有设备和所有版本的 Android。

回答by Christopher Orr

This is pretty much a duplicate of: Android ListView Selector Color

这几乎是重复的:Android ListView Selector Color

Also, why do you need to detect the colours? Just do nothingand your widgets will fit in to the platform's existing look & feel.

另外,为什么需要检测颜色?什么都不做,您的小部件将适合平台现有的外观和感觉。

Or if you're writing a custom theme, just make yours inherit from android:Theme.

或者,如果您正在编写自定义主题,只需让您的主题从android:Theme.