安卓UI布局之checkbox

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

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

猜你喜欢

转载自blog.csdn.net/m0_37705108/article/details/84350218