编写不易,如有转载,请声明出处:http://blog.csdn.net/zxc514257857/article/details/60480014
Demo展示图片
添加依赖
在project的build.gradle—>buildscript—>repositories节点下添加:
mavenCentral()
如图:
在module的dependencies节点下添加:
compile 'com.github.zcweng:switch-button:0.0.3@aar'
如图:
布局代码
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
tools:context=".MainActivity">
<!--默认的switchbutton-->
<com.suke.widget.SwitchButton
android:id="@+id/switchButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<!--设置被打开-->
<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"/>
</LinearLayout>
activity代码
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.suke.widget.SwitchButton;
import butterknife.Bind;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity implements SwitchButton.OnCheckedChangeListener {
@Bind(R.id.switchButton1)
SwitchButton mSwitchButton1;
@Bind(R.id.switchButton2)
SwitchButton mSwitchButton2;
@Bind(R.id.switchButton3)
SwitchButton mSwitchButton3;
@Bind(R.id.switchButton4)
SwitchButton mSwitchButton4;
@Bind(R.id.switchButton5)
SwitchButton mSwitchButton5;
@Bind(R.id.switchButton6)
SwitchButton mSwitchButton6;
@Bind(R.id.switchButton7)
SwitchButton mSwitchButton7;
@Bind(R.id.switchButton8)
SwitchButton mSwitchButton8;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
mSwitchButton1.setOnCheckedChangeListener(this);
}
@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);
}
}
}
注
Demo中还依赖了:
compile 'com.jakewharton:butterknife:7.0.1'
如想对SwitchButton布局进行更多修改,可移步SwitchButton的GitHub地址:https://github.com/zcweng/SwitchButton
Demo下载请移步:http://download.csdn.net/detail/zxc514257857/9893023
----------因本人才疏学浅,如博客或Demo中有错误的地方请大家随意指出,与大家一起讨论,共同进步,谢谢!----------