Fixed: “Android: Detecting focus/pressed color”

后端 未结 3 1257
离开以前
离开以前 2020-12-25 08:17

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 par

相关标签:
3条回答
  • 2020-12-25 08:24

    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.

    0 讨论(0)
  • 2020-12-25 08:24

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

    Also, why do you need to detect the colours? Just do nothing and 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.

    0 讨论(0)
  • 2020-12-25 08:43

    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:

      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.

    0 讨论(0)
提交回复
热议问题