简体   繁体   中英

Programmatically styling Android's CheckBox

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.

您可以设置样式以使用ContextThemeWrapper以编程方式查看。

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

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!

This is my working solution:

CheckBox checkBox = new CheckBox(new ContextThemeWrapper(this, R.style.CheckBoxStyle))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM