【发布时间】:2011-04-27 07:12:56
【问题描述】:
是否有一种简单的方法可以为复选框使用自定义图像?我希望复制 gmail 的“加星标”行为。所以我想拥有一个复选框,当检查时,是一个填充的明星。未选中时是一个空星。我是否必须使用图像视图并自己做我自己的逻辑?
【问题讨论】:
是否有一种简单的方法可以为复选框使用自定义图像?我希望复制 gmail 的“加星标”行为。所以我想拥有一个复选框,当检查时,是一个填充的明星。未选中时是一个空星。我是否必须使用图像视图并自己做我自己的逻辑?
【问题讨论】:
创建一个可绘制的复选框选择器:
<?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>
确保您的复选框是这样的android:button="@drawable/checkbox_selector"
<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" />
【讨论】:
【讨论】:
android:button的解决方案比使用背景属性好很多!
将 btn_check.xml 从 android-sdk/platforms/android-#/data/res/drawable 复制到项目的可绘制文件夹,并将“打开”和“关闭”图像状态更改为自定义图像。
那么你的 xml 将只需要 android:button="@drawable/btn_check"
<CheckBox
android:button="@drawable/btn_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true" />
如果你想使用不同的默认android图标,你可以使用android:button="@android:drawable/..."
【讨论】:
res/drawable/day_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/dayselectionunselected"
android:state_checked="false"/>
<item android:drawable="@drawable/daysselectionselected"
android:state_checked="true"/>
<item android:drawable="@drawable/dayselectionunselected"/>
</selector>
res/layout/my_layout.xml
<CheckBox
android:id="@+id/check"
android:layout_width="39dp"
android:layout_height="39dp"
android:background="@drawable/day_selector"
android:button="@null"
android:gravity="center"
android:text="S"
android:textColor="@color/black"
android:textSize="12sp" />
【讨论】:
如果您有 Android 开源代码,您可以在以下位置找到样式定义:
src/frameworks/base/core/res/res/values
<style name="Widget.CompoundButton.CheckBox">
<item name="android:background">
@android:drawable/btn_check_label_background
</item>
<item name="android:button">
?android:attr/listChoiceIndicatorMultiple
</item>
</style>
【讨论】:
基于 Enselic 和 Rahul 的答案。
它适用于我(在 API 21 之前和之后):
<CheckBox
android:id="@+id/checkbox"
android:layout_width="40dp"
android:layout_height="40dp"
android:text=""
android:gravity="center"
android:background="@drawable/checkbox_selector"
android:button="@null"
app:buttonCompat="@null" />
【讨论】:
试试看 -
package com;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
public class CheckBoxImageView extends ImageView implements View.OnClickListener {
boolean checked;
int defImageRes;
int checkedImageRes;
OnCheckedChangeListener onCheckedChangeListener;
public CheckBoxImageView(Context context, AttributeSet attr, int defStyle) {
super(context, attr, defStyle);
init(attr, defStyle);
}
public CheckBoxImageView(Context context, AttributeSet attr) {
super(context, attr);
init(attr, -1);
}
public CheckBoxImageView(Context context) {
super(context);
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
setImageResource(checked ? checkedImageRes : defImageRes);
}
private void init(AttributeSet attributeSet, int defStyle) {
TypedArray a = null;
if (defStyle != -1)
a = getContext().obtainStyledAttributes(attributeSet, R.styleable.CheckBoxImageView, defStyle, 0);
else
a = getContext().obtainStyledAttributes(attributeSet, R.styleable.CheckBoxImageView);
defImageRes = a.getResourceId(0, 0);
checkedImageRes = a.getResourceId(1, 0);
checked = a.getBoolean(2, false);
a.recycle();
setImageResource(checked ? checkedImageRes : defImageRes);
setOnClickListener(this);
}
@Override
public void onClick(View v) {
checked = !checked;
setImageResource(checked ? checkedImageRes : defImageRes);
onCheckedChangeListener.onCheckedChanged(this, checked);
}
public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
this.onCheckedChangeListener = onCheckedChangeListener;
}
public static interface OnCheckedChangeListener {
void onCheckedChanged(View buttonView, boolean isChecked);
}
}
添加此属性 -
<declare-styleable name="CheckBoxImageView">
<attr name="default_img" format="integer"/>
<attr name="checked_img" format="integer"/>
<attr name="checked" format="boolean"/>
</declare-styleable>
使用喜欢 -
<com.adonta.ziva.consumer.wrapper.CheckBoxImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/checkBox"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:clickable="true"
android:padding="5dp"
app:checked_img="@drawable/check_box_checked"
app:default_img="@drawable/check_box" />
它会解决你所有的问题。
【讨论】:
onSaveInstanceState() 和onRestoreInstanceState() 方法,我认为检查状态会在轮换时丢失
另一种选择是使用带有空背景和自定义按钮的ToggleButton。
下面是一个包含文本颜色选择器的示例。
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/toggle_selector"
android:background="@null"
android:paddingLeft="10dp"
android:layout_centerHorizontal="true"
android:gravity="center"
android:textColor="@drawable/toggle_text"
android:textOn="My on state"
android:textOff="My off state" />
toggle_selector.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/state_on" />
<item
android:drawable="@drawable/state_off" />
</selector>
toggle_text.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:color="@color/app_color" />
<item
android:color="@android:color/darker_gray" />
</selector>
【讨论】:
如果您使用自定义适配器而不是 android:focusable="false" 和 android:focusableInTouchMode="false" ,则必须在使用复选框时使列表项可点击。
<CheckBox
android:id="@+id/checkbox_fav"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/checkbox_layout"/>
在drawable>checkbox_layout.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/uncked_checkbox"
android:state_checked="false"/>
<item android:drawable="@drawable/selected_checkbox"
android:state_checked="true"/>
<item android:drawable="@drawable/uncked_checkbox"/>
</selector>
【讨论】:
如果您使用 androidx.appcompat:appcompat 并希望自定义可绘制对象(类型为 selector 和 android:state_checked)在旧平台版本和新平台版本上工作,您需要使用
<CheckBox
app:buttonCompat="@drawable/..."
而不是
<CheckBox
android:button="@drawable/..."
【讨论】:
在 Material Checkbox version-1.3.0 的 android:button 中添加自定义 drawable 对我不起作用。我不得不设置 android:drawable="@drawable/checkbox_selector" 并设置 android:button="@null" 。您还可以添加android:drawablePadding 使其看起来不错。但是,这会使整个复选框(连同文本)都可以点击。
【讨论】: