programing

사용자 지정 확인란 이미지 안드로이드

yoursource 2023. 8. 15. 15:19
반응형

사용자 지정 확인란 이미지 안드로이드

확인란에 사용자 지정 이미지를 쉽게 사용할 수 있는 방법이 있습니까?저는 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" />

확인란이 단추의 자식인 경우, 여기에 설명된 대로 "단추 스타일" 아래에 있는 여러 상태의 배경 이미지를 확인란에 제공하면 됩니다.

...여기에 예시되어 있습니다.

btn_check.xml을 Android-sdk/platforms/android-#/data/res/drawable에서 프로젝트의 그리기 가능 폴더로 복사하고 'on' 및 'off' 이미지 상태를 사용자 지정 이미지로 변경합니다.

그러면 당신의 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:button="@android:drawable/..."

res/drawable/day_drawable.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/message/my_message.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/sb/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" />

그것은 당신의 모든 문제를 해결해 줄 것입니다.

다른 옵션은 Null 배경과 사용자 지정 단추가 있는 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_toggle.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"/>

그리기 가능>체크박스_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/..."

Android에서 사용자 지정 그리기 가능 추가: 재료 확인란에서 버튼이 작동하지 않았습니다.version-1.3.0설정해야 했습니다.android:drawable="@drawable/checkbox_selector"대신에 또한 세트android:button="@null"추가할 수도 있습니다.android:drawablePadding잘 보이게 하기 위해서.그러나 이렇게 하면 텍스트와 함께 전체 확인란을 클릭할 수 있습니다.

선택한 상태와 선택하지 않은 상태 또는 기본 상태에 대해 그릴 수 있는 두 개의 그림이 필요합니다.그런 다음 배경 그리기 가능한 선택기(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/ic_checkbox_checked_24" />
    <item
        android:state_checked="false"
        android:drawable="@drawable/ic_checkbox_unchecked_24" />
    <item
        android:drawable="@drawable/ic_checkbox_unchecked_24"/>
</selector>

xml의 확인란이 이렇게 표시되지 않아야 합니다.

<CheckBox
      android:id="@+id/check_box"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:button="@null"
      android:background="@drawable/checkbox_background_drawable"/>

확인란의 버튼 특성을 null로 설정했는지 확인합니다.

언급URL : https://stackoverflow.com/questions/3965484/custom-checkbox-image-android

반응형