Android上的着色复选框和单选按钮
我有一个Android天文学应用程序,我需要将UI染成红色,以便在夜间使用。虽然我有一个适合许多(大多数???)UI元素的方案,但我在使用复合按钮时遇到了问题:复选框和单选按钮 基本思想是检索UI元素的可绘制内容,如果有,则在其上设置颜色过滤器。我的问题是为复合按钮找到合适的可绘制按钮。我认为getCompoundDrawables()是我需要的,但是4个drawables的返回数组总是包含4个元素的nullAndroid上的着色复选框和单选按钮,android,colors,checkbox,filter,drawable,Android,Colors,Checkbox,Filter,Drawable,我有一个Android天文学应用程序,我需要将UI染成红色,以便在夜间使用。虽然我有一个适合许多(大多数???)UI元素的方案,但我在使用复合按钮时遇到了问题:复选框和单选按钮 基本思想是检索UI元素的可绘制内容,如果有,则在其上设置颜色过滤器。我的问题是为复合按钮找到合适的可绘制按钮。我认为getCompoundDrawables()是我需要的,但是4个drawables的返回数组总是包含4个元素的null public static void setNightVisionBkg( View
public static void setNightVisionBkg( View view )
{
if ( view instanceof ViewGroup )
{
Drawable drawable = view.getBackground();
if ( drawable != null )
drawable.setColorFilter( 0xFFAA0000, PorterDuff.Mode.MULTIPLY );
ViewGroup group = (ViewGroup) view;
int numChildren = group.getChildCount();
for ( int i = 0; i < numChildren; i++ )
{
View v = group.getChildAt( i );
Drawable d = v.getBackground();
if ( d != null )
d.setColorFilter( 0xFFAA0000, PorterDuff.Mode.MULTIPLY );
if ( v instanceof ViewGroup )
{
setNightVisionBkg( (ViewGroup) v );
}
else if (v instanceof CompoundButton)
{
CompoundButton compBtn = (CompoundButton)v;
Drawable drawables[] = compBtn.getCompoundDrawables();
for (int j = 0; j < drawables.length; j++)
if (drawables[j] != null)
{
drawables[j].setColorFilter( 0xFFAA0000, PorterDuff.Mode.MULTIPLY );
}
}
}
}
}
下面是我在顶层视图中调用的递归代码,以尝试为UI元素着色
public static void setNightVisionBkg( View view )
{
if ( view instanceof ViewGroup )
{
Drawable drawable = view.getBackground();
if ( drawable != null )
drawable.setColorFilter( 0xFFAA0000, PorterDuff.Mode.MULTIPLY );
ViewGroup group = (ViewGroup) view;
int numChildren = group.getChildCount();
for ( int i = 0; i < numChildren; i++ )
{
View v = group.getChildAt( i );
Drawable d = v.getBackground();
if ( d != null )
d.setColorFilter( 0xFFAA0000, PorterDuff.Mode.MULTIPLY );
if ( v instanceof ViewGroup )
{
setNightVisionBkg( (ViewGroup) v );
}
else if (v instanceof CompoundButton)
{
CompoundButton compBtn = (CompoundButton)v;
Drawable drawables[] = compBtn.getCompoundDrawables();
for (int j = 0; j < drawables.length; j++)
if (drawables[j] != null)
{
drawables[j].setColorFilter( 0xFFAA0000, PorterDuff.Mode.MULTIPLY );
}
}
}
}
}
public静态无效setNightVisionBkg(视图)
{
if(视图组的视图实例)
{
Drawable Drawable=view.getBackground();
如果(可绘制!=null)
可绘制的.setColorFilter(0xFFAA0000,PorterDuff.Mode.MULTIPLY);
视图组组=(视图组)视图;
int numChildren=group.getChildCount();
for(int i=0;i
请注意,在后一部分中,它为无法工作的CompoundButton获取可提取项(所有可提取项均为null)
你有什么想法吗?我知道我可以设置自己的自定义绘图,但我更喜欢使用标准绘图,如果可能的话,只需设置一个颜色过滤器。您不会处理“正常”非视图组视图。还有你的测试
for for ViewGroup在for循环中,只有当视图是ViewGroup时才能运行。我用稍微不同的方法解决了我的问题。最终子类化了复选框(和RadioButton)。在子类I中,覆盖:
protected boolean verifyDrawable( Drawable drawable )
在这个方法中,我在绘图板上设置了颜色过滤器。非常好。不,非视图组视图确实得到了处理(您说过已进行,但我想这就是您的意思)。设置过滤器的代码确实会为复选框调用,但它没有任何影响,因为所有可绘制的内容都为null。我已经在调试器中验证了这一点。视图组的内部测试是针对孩子的,所以我不认为这是一个多余的测试。