Android自定义Switch View控件CySwitch

Android自带的Switch控件其实挺好看的,但因各版本系统显示效果不太相同,而且就国内UI视觉审美环境来说,该控件很难受宠,往往需要自行定义Switch View以满足项目的需求。由于该控件实现比较简单,本文只作开源库的分享和介绍,希望能给大家带来帮助。

CySwitch开源库地址:https://github.com/ausboyue/CySwitch

效果演示

效果演示

在项目中使用

  1. Layout XMl 布局中代码示例:
<cn.icheny.view.CySwitch
    android:id="@+id/cy_switch"
    android:layout_width="120dp"
    android:layout_height="50dp"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="25dp"
    app:borderColorChecked="@android:color/holo_green_dark"
    app:borderColorUnchecked="@android:color/holo_blue_light"
    app:borderWidth="0dp"
    app:isChecked="true"
    app:sliderHeight="40dp"
    app:sliderRadius="0dp"
    app:sliderWidth="40dp"
    app:viewRadius="0dp" />
  1. java代码示例:
CySwitch cy_switch = findViewById(R.id.cy_switch);
cy_switch.setViewRadius(radius);
cy_switch.setSliderRadius(radius);
cy_switch.setBorderWidth(width);
cy_switch.checkable(true);
cy_switch.setOnCheckedChangeListener(new CySwitch.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CySwitch switchView, boolean isChecked) {
        Toast.makeText(MainActivity.this, "Switch " + isChecked, Toast.LENGTH_SHORT).show();
    }
});

属性介绍

属性描述
isChecked是否为Checked状态
switchable是否可以切换
duration动画时间间隔
viewRadius控件圆角
borderWidth边框宽度
borderColorChecked边框颜色 Checked
borderColorUnchecked边框颜色 Unchecked
bgColorChecked背景颜色 Checked
bgColorUnchecked背景颜色 Unchecked
sliderColorChecked滑块颜色 Checked
sliderColorUnchecked滑块颜色 Unchecked
sliderRadius滑块圆角
sliderWidth滑块宽
sliderHeight滑块高

后期会添加切换时的颜色过渡,shadow阴影以及点击水波纹效果,敬请期待。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值