通过代码添加的Android复选框的外观不正确? [英] Android checkboxes added by code don't have the right appearance?
问题描述
我有一个非常基本的应用程序,我试图在其中插入动态复选框,但我可以让它们显示出来,但是它们没有以正确的样式显示.
I've got a really basic app in which I'm trying to insert dynamic checkboxes, and I can get them to appear, but they're not showing with the correct styling.
请参见下文-Foo根据定义位于LinearLayout中;栏正在以编程方式添加. Foo显示带有灰色框的检查,Bar显示带有白色框.
See below - Foo is in the LinearLayout by definition; Bar is being added programmatically. Foo is showing with a grey box for the check, Bar is showing with a white box.
下面是创建这些代码的代码:
Here's the code that's creating these:
for (Integer i=0; i < arArray.length;i++) {
CheckBox cb = new CheckBox(getApplicationContext());
cb.setText("Bar");
cb.setTextColor(getResources().getColor(android.R.color.black));
cb.setOnCheckedChangeListener(clListener);
llDeckChecks.addView(cb);
}
我尝试将drawable设置为某些android.R.drawable类型,但没有与Foo复选框匹配的内容,因此在这一点上我完全感到困惑.
I've tried setting the drawable to some android.R.drawable types, but nothing matches the Foo checkbox, so I'm entirely stumped at this point.
推荐答案
您应该使用活动上下文而不是应用程序上下文来创建按钮.如果使用getApplicationContext()
,则不会扩展ContextThemeWrapper
.
You should use the activity context to create the button instead of the application context. If you use getApplicationContext()
it does not extend ContextThemeWrapper
.
尝试更改
CheckBox cb = new CheckBox(getApplicationContext());
到
CheckBox cb = new CheckBox(YourActivity.this); // or getActivity() if in a fragment.
这篇关于通过代码添加的Android复选框的外观不正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!