安卓UI布局之checkbox

本文详细介绍如何在Android中使用自定义背景选择器为CheckBox创建独特的视觉效果,包括布局文件配置及drawable资源设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先看一下效果图:在这里插入图片描述
布局文件如下如所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.hp.practicedemo.MainActivity">

    <FrameLayout
        android:id="@+id/et_setting_protocol_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <CheckBox
            android:id="@+id/et_public_cloud_tcp_udp_switch"
            android:layout_width="385dp"
            android:layout_height="wrap_content"
            android:background="@drawable/et_default_custom_bg_selector"
            android:clickable="true"
            android:focusable="true"
            android:button="@null"
            android:focusableInTouchMode="false"
            android:padding="6dp"  />

        <TextView android:id="@+id/et_public_cloud_udp_word"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="78dp"
            android:text="UDP"
            android:textSize="20dp"
            android:textColor="#ff0000"
            android:enabled="true" />

        <TextView android:id="@+id/et_public_cloud_tcp_word"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="261dp"
            android:text="TCP"
            android:textSize="20dp"
            android:textColor="#ff0000"
            android:enabled="false"/>
    </FrameLayout>

</LinearLayout>

在drawable目录下面创建et_default_custom_bg_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:state_focused="true" android:drawable="@drawable/et_setting_advanced_right_focus"/>
    <item android:state_checked="true" android:state_focused="false" android:drawable="@drawable/et_setting_advanced_right_normal"/>
    <item android:state_checked="false" android:state_focused="true" android:drawable="@drawable/et_setting_advanced_left_focus"/>
    <item android:state_checked="false" android:state_focused="false" android:drawable="@drawable/et_setting_advanced_left_normal"/>
   
</selector>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值