仿ios样式的SwitchButton

1.添加依赖

implementation 'com.kyleduo.switchbutton:library:1.4.1'

 

2.布局

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"

android:orientation="vertical"

tools:context="com.example.zb.myswitchbuttondemo.MainActivity">

 

android:id="@+id/switchButton"

style="@style/SwitchButtonStyle"

android:layout_width="80dp"

android:layout_height="50dp"

android:layout_centerInParent="true"

android:checked="false"

app:kswAnimationDuration="300"

app:kswBackDrawable="@drawable/ios_back_drawable"

app:kswBackMeasureRatio="1.4"

app:kswThumbDrawable="@drawable/ios_thumb_selector"

app:kswThumbMarginBottom="-8dp"

app:kswThumbMarginLeft="-5dp"

app:kswThumbMarginRight="-5dp"

app:kswThumbMarginTop="-2.5dp" />

 

 

3.style="@style/SwitchButtonStyle"

 

4.@drawable/ios_back_drawable

android:width="1.5dp"

android:color="#F0F0F0"/>

android:width="1.5dp"

android:color="#E6E6E6"/>

 

5.@drawable/ios_thumb_selector

 

 

6.MainActivity

 

package com.example.zb.myswitchbuttondemo;

 

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.CompoundButton;

import android.widget.Toast;

 

import com.kyleduo.switchbutton.SwitchButton;

 

public class MainActivity extends AppCompatActivity {

 

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

SwitchButton mSwitchButton = (SwitchButton) findViewById(R.id.switchButton);

mSwitchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override

public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

String s;

if (b)

s = "确定";

else

s = "取消";

 

Toast.makeText(getApplication(), s, Toast.LENGTH_SHORT).show();

}

});

 

}

}

 

 

github地址:https://github.com/b888zhang/MySwitchButtonDemo

你可能感兴趣的:(Android笔记)