复选框颜色更改 android
Checkbox color change android
我有一个复选框,我想更改它的颜色。我附上一张照片以获得更多理解。
我想将黑色方块颜色更改为白色,就像文本 "Press to skip" 一样。我的 xml 代码如下:
<CheckBox
android:id="@+id/checkBox1"
android:background="@drawable/tutorial_screen_edit_text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCheckboxClicked"
android:text="Press to Skip"
android:textColor="@color/white"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
在 drawalbe 文件夹中定义你自己的 drawalbe
checkbox_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/checkbox"
android:state_checked="false"/>
<item android:drawable="@drawable/checkboxselected"
android:state_checked="true"/>
<item android:drawable="@drawable/checkbox"/>
</selector>
your_layout.xml
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:button="@drawable/checkbox_selector"
android:text="CheckBox"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/Black" />
尝试使用 ColorStateList
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_checked}, // unchecked
new int[]{android.R.attr.state_checked} , // checked
},
new int[]{
Color.parseColor("#000000"), //unchecked color
Color.parseColor("#FFFFFF"), //checked color
}
);
设置颜色使用:setButtonTintList()
CheckBox cb = (CheckBox) findViewById(R.id.checkBox1);
CompoundButtonCompat.setButtonTintList(cb,colorStateList);
试试这个。
<CheckBox
android:id="@+id/chk_remember_signup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@android:color/white"
android:text="hello" />
答案很简单,请查看下面的代码片段:
<CheckBox
android:id="@+id/checkBox1"
android:background="@drawable/tutorial_screen_edit_text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCheckboxClicked"
android:text="Press to Skip"
android:buttonTint="@color/white" <!--just add this line-->
android:textColor="@color/white"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
我有一个复选框,我想更改它的颜色。我附上一张照片以获得更多理解。
我想将黑色方块颜色更改为白色,就像文本 "Press to skip" 一样。我的 xml 代码如下:
<CheckBox
android:id="@+id/checkBox1"
android:background="@drawable/tutorial_screen_edit_text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCheckboxClicked"
android:text="Press to Skip"
android:textColor="@color/white"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
在 drawalbe 文件夹中定义你自己的 drawalbe
checkbox_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/checkbox"
android:state_checked="false"/>
<item android:drawable="@drawable/checkboxselected"
android:state_checked="true"/>
<item android:drawable="@drawable/checkbox"/>
</selector>
your_layout.xml
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:button="@drawable/checkbox_selector"
android:text="CheckBox"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/Black" />
尝试使用 ColorStateList
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_checked}, // unchecked
new int[]{android.R.attr.state_checked} , // checked
},
new int[]{
Color.parseColor("#000000"), //unchecked color
Color.parseColor("#FFFFFF"), //checked color
}
);
设置颜色使用:setButtonTintList()
CheckBox cb = (CheckBox) findViewById(R.id.checkBox1);
CompoundButtonCompat.setButtonTintList(cb,colorStateList);
试试这个。
<CheckBox
android:id="@+id/chk_remember_signup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@android:color/white"
android:text="hello" />
答案很简单,请查看下面的代码片段:
<CheckBox
android:id="@+id/checkBox1"
android:background="@drawable/tutorial_screen_edit_text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCheckboxClicked"
android:text="Press to Skip"
android:buttonTint="@color/white" <!--just add this line-->
android:textColor="@color/white"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />