https://github.com/kyleduo/SwitchButton
1、导包
implementation 'com.kyleduo.switchbutton:library:2.0.0'
2、使用
mAnimationAdapter.isFirstOnly(false);//init firstOnly state
SwitchButton switchButton = (SwitchButton) findViewById(R.id.switch_button);
switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
if (isChecked) {
mAnimationAdapter.isFirstOnly(true);
} else {
mAnimationAdapter.isFirstOnly(false);
}
mAnimationAdapter.notifyDataSetChanged();
}
});
3、布局
<com.kyleduo.switchbutton.SwitchButton
android:id="@+id/switch_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
app:kswTextOff="false"
app:kswTextOn="true"
android:textColor="@drawable/custom_text_state_color"
app:kswThumbDrawable="@drawable/thumb_drawable"
app:kswThumbColor="#555771"
app:kswTintColor="#00ddB6"
app:kswThumbWidth="30dp"
app:kswThumbHeight="30dp"
app:kswThumbRadius='0dp'
/>
4、定制
在xml布局文件中配置属性.
name | info |
---|---|
kswThumbDrawable | 手柄 |
kswThumbColor | 手柄颜色 |
kswThumbMargin | 手柄周边margin |
kswThumbMarginTop | 上外边距 |
kswThumbMarginBottom | 底外边距 |
kswThumbMarginLeft | 左外边距 |
kswThumbMarginRight | 右外边距 |
kswThumbWidth | 手柄宽度(与高度等时是个圆) |
kswThumbHeight | 手柄高度 |
kswThumbRadius | 手柄的圆角 需要:kswThumbColor |
kswBackRadius | 除手柄外的背景色需要:kswBackColor |
kswBackDrawable | 背景图 |
kswBackColor | 背景字体颜色 |
kswFadeBack | 启用动画? |
kswAnimationDuration | 动画持续时间 |
kswTintColor | change SwitchButton’s style just by one property, all relevant color will be generate automatically. Do not support SwitchButtonMD or other style created by xml resources. |
kswTextOn | 开文字 |
kswTextOff | 关文字 |
kswTextThumbInset (since 2.0.0) | length of the part of text under the thumb. |
kswTextExtra (since 2.0.0) | extra space needed by background besides the actual text width. |
kswTextAdjust (since 2.0.0) | 文字自适应 |
kswThumbRangeRatio (since 2.0.0) | 手柄移动范围长条滑动 |
kswBackMeasureRatio | (since 2.0.0)移除 |
kswTextMarginH | (since 2.0.0)移除 |
kswAutoAdjustTextPosition | (since 2.0.0)移除 |
private String[] opts = new String[]{
"setThumbColorRes/setThumbColor",
"setThumbDrawableRes/setThumbDrawable",
"setBackColorRes/setBackColor",
"setBackDrawableRes/setBackDrawable",
"setTintColor",
"setThumbMargin",
"setThumbSize",
"setThumbRadius (color-mode only)",
"setBackRadius (color-mode only)",
"setFadeBack",
"setBackMeasureRatio",
"setAnimationDuration",
"setDrawDebugRect",
"setText",
};