且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

修正:"安卓检测对焦/ pressed颜色"

更新时间:2021-12-31 08:22:08

我有这个问题了。正如已经说过的,问题是,背景不是简单的颜色,它们可绘制,可以采取对所有种出场。然而,我发现了一个变通办法,可能会有所帮助。如果您的自定义组件看起来像一个已经存在的,如一个按钮或ListView项,你可以窃取他们的背景/选择器,并设置为背景的自定义组件。例如,在你的自定义组件的构造函数:

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());

您可能要稍微优化了code,但你的想法。

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