안드로이드 CheckBox Custom

김희영·2023년 12월 2일

Android Studio

목록 보기
2/2

안드로이드 기본 체크박스 아닌 원하는 체크박스 모양으로 커스텀하는 방법

Selector 만들기

res/drawable 폴더에 새로운 xml파일을 생성한다.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 체크박스 해제 상태 -->
    <item android:state_checked="false"
        android:drawable="@drawable/unchecked"/>
    <!-- 체크박스 선택 상태 -->
    <item android:state_checked="true"
        android:drawable="@drawable/checked"/>
</selector>

위와 같은 코드 작성.
체크박스 선택 여부에 따라 이미지 설정함.

layout.xml에 커스텀 리소스파일 추가

android:background="@drawable/custom_checkbox"
android:button="@null"

CheckBox를 커스텀할 때
android_button="@null" or android:button="@android:color/transparent" 를 작성하여 기존 체크박스를 투명색으로 지정해야 커스텀된 체크박스가 크기에 맞게 들어감.

reference
https://salix97.tistory.com/227
https://dev-minsuuuus.tistory.com/30

profile
서버 & 안드로이드 steadiness-!!!

0개의 댓글