Programmatically styling Android's CheckBox

有些话、适合烂在心里 提交于 2020-02-03 05:41:17

问题


I got a class which extends Activity and programmatically adding some View items.

CheckBox checkBox = new CheckBox(this, null, android.R.style.Widget_CompoundButton_Star);

I'd like to set some checkBoxes styled as the Android SDK stars. I can't see any check box after I set its style to that Widget_CompoundButton_Star. Although that SDK style apparently works when directly placing in a xml.

Am I missing something here? THX

// As Pragnani said. Using android.R.attr.starStyle instead works.


回答1:


I've arrived here looking for styling the text of the CheckBox instead of the Drawable, and I find the solution by another way. But hey! Let me share the solution to the myself of the future.

Instead of:

CheckBox cb = new CheckBox(this, null, R.style.yourstyle);

Use:

CheckBox cb = new CheckBox(this);
cb.setTextAppearance(this, R.style.yourstyle);

Hope it helps to solve the topic of the question!




回答2:


You can set style to view programmatically with ContextThemeWrapper.

CheckBox mCheckBox = new CheckBox(new ContextThemeWrapper(context, R.style.MyCheckBox));


来源:https://stackoverflow.com/questions/14963571/programmatically-styling-androids-checkbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!