我需要通过以下方式将选择器设置为以编程方式创建的复选框:
var cbAll = new CheckBox(Activity);
LinearLayout.LayoutParams llAll = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
cbAll.Text = "All zones";
llAll.LeftMargin = 27;
llAll.TopMargin = 24;
cbAll.ButtonDrawable = //what to put here?
我的选择器(名为cb_edit.xml)在可绘制文件夹中:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/cb_active" android:state_checked="true" />
<item android:drawable="@drawable/cb_default" android:state_checked="false"/>
</selector>
答案 0 :(得分:1)
我不知道您到底要尝试什么。但是如果您设置背景,请尝试
cbAll.SetBackgroundResource( Resource.Drawable.cb_edit);
答案 1 :(得分:0)
CheckBoxDrawable.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/CheckedImage" />
<item android:state_checked="false" android:drawable="@drawable/UnCheckedImage" />
</selector>
然后声明该复选框,并将按钮设置为可绘制的,如下所示:
Checkbox btnOption = new Checkbox(context);
btnOption.SetButtonDrawable(Resource.Drawable.CheckBoxDrawable);
在不还原的情况下应该可以正常工作