Android中的自定义圆形复选框 [英] Customized circular checkbox in Android
问题描述
我正在尝试让Google Apps照片选择用户界面..我正在使用Appcompat复选框来实现这一目标,但没有成功.我正在努力的步骤, 1.将复选框背景设置为自定义圆形 2.在xml中定义自定义形状
I am trying to get the Google apps photo select UI .. Am using Appcompat checkbox to achieve that with out success. The steps I am working on , 1. Set the checkbox background to custom circular shape 2. define custom shape in xml
这是我的xml复选框,
This is my check box xml looks like ,
<android.support.v7.widget.AppCompatCheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkBox"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:button="@drawable/custom_checkbox"
android:background="@drawable/checkbox_drawable"
/>
我的自定义复选框背景,
My custom checkbox background,
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="1dp"
android:color="#78d9ff"/>
<solid
android:color="#79bfea"/>
</shape>
我的复选框按钮
<selector>
<item android:drawable="@drawable/checkbox_drawable"
android:state_checked="false"/>
<item android:drawable="@drawable/selected_check"
android:state_checked="true"/>
</selector>
我什至从android:background更改为android:button ..什么也没有给我圆形复选框..感谢您的帮助吗?我应该使用浮动操作栏吗?或观点?有什么建议吗?
I even changed from android:background to android:button .. nothing gives me the circular check box .. Any help is appreciated ? Should I use floating action bar ? or a view ? Any suggestions ?
推荐答案
带有两个可绘制图像的自定义复选框.
1..在 .xml 文件
<android.support.v7.widget.AppCompatCheckBox
android:id="@+id/checkBox"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:background="@drawable/checkbox_selector"
android:button="@color/white"
android:checked="true" />
2..在 drawable 文件夹中创建 checkbox_selector.xml .
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/cancel"
android:state_checked="false"/>
<item android:drawable="@drawable/checked"
android:state_checked="true"/>
</selector>
3..通过以下链接在可绘制文件夹中添加 checked.png 和 cansel.phd 图像
3. Add checked.png and cansel.phd image in drawable folder from the below link
https://icons8.com/icon/set/yes/all
还有
https://icons8.com/icon/set/cross/all
4..复选框 click事件
AppCompatCheckBox checkBox = (AppCompatCheckBox) view.findViewById(R.id.checkBox);
checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (checkBox.isChecked()){
checkBox.setChecked(false);
}else {
checkBox.setChecked(true);
}
}
});
这篇关于Android中的自定义圆形复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!