首页 > 解决方案 > 如何以编程方式将选择器设置为复选框

问题描述

我需要将选择器设置为以编程方式创建的复选框,这样:

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>

标签: xamarinxamarin.android

解决方案


我不知道你到底在尝试什么。但是如果你设置背景然后试试这个

cbAll.SetBackgroundResource( Resource.Drawable.cb_edit); 

推荐阅读