[英]Define a selector state in java
I would like to define a selector dynamically in java. 我想在Java中动态定义选择器。
I use drawables from ressources and above I write some text. 我使用来自资源的可绘制对象,并且上面我写了一些文本。 So, both normal drawable and drawable focus state cannot be defined into an xml file... 因此,常规可绘制状态和可绘制焦点状态都无法定义到xml文件中。
Is there a way to do this in Java? 有没有办法用Java做到这一点?
Have you checked out StateListDrawable ? 您已签出StateListDrawable吗?
This is the type of drawable that will be created by your selector. 这是选择器将创建的可绘制类型。
So you could do something like this: 因此,您可以执行以下操作:
StateListDrawable drawable = new StateListDrawable();
drawable.addState(new int[]{android.R.attr.state_focused}, mFocusedDrawable);
drawable.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed}, mEnabledPressedDrawable);
drawable.addState(new int[0], mDefaultDrawable);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.