【Android开发--新手必看篇】Switch 开关(含自定义样式)

Android笔记

​ ——各类控件的使用(控件)-开关
若对该知识点有更多想了解的,欢迎私信博主~~

开关:Switch
属性:
XML属性说明
layout_width布局宽度
layout_height布局高度
switchMinWidth设置开关的最小宽度
textOn按钮被选中时显示的文字
textOff按钮没有被选中时显示的文字
checked是否被选中
track开关轨道的颜色
thumb开关按钮的颜色
showText设置on/off的时候是否显示文字
splitTrack是否设置一个间隙,让滑块与底部图片分隔
注:textOn和textOff使用时showText必须为true
方法:
常用方法说明
isChecked()被选中
高级方法:当前状态更改时触发
	Switch aSwitch;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main26);
        //绑定控件
        aSwitch = findViewById(R.id.switch1);

        //当前状态更改时触发
        aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                Toast.makeText(Main26Activity.this, "被改变了", Toast.LENGTH_SHORT).show();
            }
        });
    }
高级样式:按钮与轨道的样式改变
  1. 按钮(此文件名thumb)

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true">
            <shape xmlns:android="http://schemas.android.com/apk/res/android"
                android:shape="rectangle">
    
                <!--高度40-->
                <size
                    android:width="40dp"
                    android:height="40dp" />
                <!--圆角弧度20-->
                <corners android:radius="20dp" />
                <!--变化率-->
                <gradient
                    android:endColor="#eeeeee"
                    android:startColor="#eeeeee" />
                <stroke
                    android:width="1dp"
                    android:color="#33da33" />
            </shape>
        </item>
        <item android:state_checked="false">
            <shape xmlns:android="http://schemas.android.com/apk/res/android"
                android:shape="rectangle">
    
                <!--高度40-->
                <size android:height="40dp" android:width="40dp" />
                <!--圆角弧度20-->
                <corners android:radius="20dp"/>
                <!--变化率-->
                <gradient
                    android:endColor="#eeeeee"
                    android:startColor="#eeeeee" />
                <stroke android:width="1dp"
                    android:color="#666666" />
            </shape>
        </item>
    </selector>
    
  2. 轨道(此文件名track)

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true">
            <shape xmlns:android="http://schemas.android.com/apk/res/android">
    
                <!--高度30此处设置宽度无效-->
                <size android:height="40dp"/>
                <!--圆角弧度15-->
                <corners android:radius="15dp"/>
                <!--变化率-->
                <gradient
                    android:endColor="#33da33"
                    android:startColor="#33da33"/>
            </shape>
        </item>
        <item android:state_checked="false">
            <shape xmlns:android="http://schemas.android.com/apk/res/android">
    
                <!--高度30此处设置宽度无效-->
                <size android:height="40dp"/>
                <!--圆角弧度15-->
                <corners android:radius="15dp"/>
                <!--变化率定义从左到右的颜色不变-->
                <gradient
                    android:endColor="#888888"
                    android:startColor="#888888"/>
            </shape>
        </item>
    </selector>
    
  3. 使用(XML样式文件中)

    <Switch
            android:id="@+id/switch1"
            android:layout_width="110dp"
            android:layout_height="160dp"
            android:checked="true"
            android:showText="true"
            android:splitTrack="true"
            android:switchMinWidth="110dp"
            android:textOff="off"
            android:textOn="on"
            android:thumb="@drawable/thumb"
            android:track="@drawable/track" />
    
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android自定义Switch控件可以通过自定义drawable来实现。以下是一个简单的例子: 1. 创建一个drawable资源文件,例如 switch_bg.xml,用于定义Switch的背景样式: ```xml <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@drawable/switch_on_bg" /> <item android:state_checked="false" android:drawable="@drawable/switch_off_bg" /> </selector> ``` 2. 创建两个drawable资源文件,例如 switch_on_bg.xml 和 switch_off_bg.xml,分别用于定义Switch开和关状态下的样式switch_on_bg.xml: ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#00FF00" /> <corners android:radius="20dp" /> </shape> ``` switch_off_bg.xml: ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FF0000" /> <corners android:radius="20dp" /> </shape> ``` 3. 在布局文件中使用自定义Switch控件: ```xml <Switch android:id="@+id/customSwitch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:thumb="@drawable/custom_switch_thumb" android:track="@drawable/switch_bg" /> ``` 其中,android:thumb属性定义了Switch的拇指(即开关按钮)的样式。我们可以创建一个自定义drawable来实现: ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#FFFFFF" /> <size android:width="20dp" android:height="20dp" /> </shape> ``` 这样就完成了自定义Switch控件的样式。当Switch状态改变时,背景样式也会随之改变。你可以根据自己的需求来修改样式

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值