仿苹果按钮效果

本文详细介绍了如何在Android中创建仿苹果风格的按钮效果,包括添加mavenCentral到项目配置,引入相关依赖,设置布局和在MainActivity中的实现,以及更多自定义选项。

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

原文地址:https://blog.youkuaiyun.com/zxc514257857/article/details/60480014

效果图

1、添加mavenCentral

在project的build.gradle—>buildscript—>repositories节点下添加mavenCentral() (:必须放在jcenter()前面)

buildscript {
    repositories {
        google()
        mavenCentral()  // 添加此句
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0'
  
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

2、添加依赖

implementation 'com.github.zcweng:switch-button:0.0.3@aar'

3、布局

<!--默认的switchbutton-->
    <com.suke.widget.SwitchButton
        android:id="@+id/switchButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

4、MainActivity

switchButton = (SwitchButton) findViewById(R.id.switchButton);
switchButton.setOnCheckedChangeListener(this);

// 按钮监听事件
    @Override
    public void onCheckedChanged(SwitchButton view, boolean isChecked) {
        if (view.isChecked()) {
            switchButton.setChecked(true);
            Toast.makeText(this, "true", Toast.LENGTH_SHORT).show();
        } else {
            switchButton.setChecked(false);
            Toast.makeText(this, "false", Toast.LENGTH_SHORT).show();
        }
}

5、更多自定义玩法

布局代码
    <!--设置被打开-->
    <com.suke.widget.SwitchButton
        android:id="@+id/switchButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:sb_checked="true"/>

    <!--设置关闭选择器-->
    <com.suke.widget.SwitchButton
        android:id="@+id/switchButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:sb_show_indicator="false"/>

    <!--设置按钮阴影颜色-->
    <com.suke.widget.SwitchButton
        android:id="@+id/switchButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:sb_shadow_color="#f00"/>

    <!--设置选中和未选中时的按钮背景颜色-->
    <com.suke.widget.SwitchButton
        android:id="@+id/switchButton5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:sb_checked_color="#f00"
        app:sb_uncheck_color="#0ff"
        app:sb_background="#0ff"/>

    <!--设置指示器选中和未选中时颜色及线宽-->
    <com.suke.widget.SwitchButton
        android:id="@+id/switchButton6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:sb_checkline_color="#f00"
        app:sb_uncheckcircle_color="#0ff"
        app:sb_checkline_width="5dp"
        app:sb_uncheckcircle_width="5dp"/>

    <!--设置按钮颜色-->
    <com.suke.widget.SwitchButton
        android:id="@+id/switchButton7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:sb_button_color="#f00"/>

    <!--设置是否启用开关特效-->
    <com.suke.widget.SwitchButton
        android:id="@+id/switchButton8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:sb_enable_effect="false"/>


**JAVA代码**
 @Override
    public void onCheckedChanged(SwitchButton view, boolean isChecked) {
        if (view.isChecked()){
            mSwitchButton2.setChecked(true);
            mSwitchButton3.setChecked(true);
            mSwitchButton4.setChecked(true);
            mSwitchButton5.setChecked(true);
            mSwitchButton6.setChecked(true);
            mSwitchButton7.setChecked(true);
            mSwitchButton8.setChecked(true);
        }else{
            mSwitchButton2.setChecked(false);
            mSwitchButton3.setChecked(false);
            mSwitchButton4.setChecked(false);
            mSwitchButton5.setChecked(false);
            mSwitchButton6.setChecked(false);
            mSwitchButton7.setChecked(false);
            mSwitchButton8.setChecked(false);
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值