android开发分享自定义checkbox图像android

有一个简单的方法来使用自定义图像的checkbox? 我正在寻找复制gmail的“主演”行为。 所以我想有一个checkbox,当选中时,是一个充满了明星。 而未经检查的是一颗空洞的明星。 我必须使用imageview并自己做自己的逻辑吗?

    checkbox是Button的子项,您可以在checkbox中select一个背景图像,其中包含如下所述的“button样式”下的几种状态:

    … 在这里举例说明:

    创build一个可绘制的checkboxselect器:

     <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="https://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> 

    确保你的checkbox是这样的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-sdk / platforms / android – #/ data / res / drawable中的btn_check.xml复制到项目的可绘制文件夹中,并将“开”和“关”图像状态更改为自定义图像。

    那么你的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/..."

    如果您有Android开源代码,您可以在下面find样式定义:
    SRC /框架/碱/核心/ RES / RES /值

     <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> 

    尝试一下 –

     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); } } 

    添加这个attrib –

     <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="https://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" /> 

    它会解决你所有的问题。

    另一种select是使用具有空背景和自定义button的ToggleButton 。

    贝娄是一个包含文本颜色select器的例子。

     <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="https://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="https://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开发分享自定义checkbox图像android相关内容,想了解更多android开发(异常处理)及android游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/addevelopment/520792.html

      (0)
      上一篇 2020年12月7日
      下一篇 2020年12月7日

      精彩推荐