Fixed: “Android: Detecting focus/pressed color”

后端 未结 3 1261
离开以前
离开以前 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: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.

提交回复
热议问题