@@ -34,197 +34,55 @@ dependencies {
3434
3535最低支持 ` Android2.2 API 7 `
3636
37- #### 准备图片
37+ #### 在布局中使用
3838
39- switch_frame.png
40-
41- ![ switch_frame.png] ( docs/switch_frame.png )
42-
43- switch_state_normal.png
44-
45- ![ switch_state_normal.png] ( docs/switch_state_normal.png )
46-
47- switch_state_disable.png
48-
49- ![ switch_state_disable.png] ( docs/switch_state_disable.png )
50-
51- switch_state_mask.png
52-
53- ![ switch_state_mask.png] ( docs/switch_state_mask.png )
54-
55- switch_slider_normal.png
39+ ``` xml
40+ <?xml version =" 1.0" encoding =" utf-8" ?>
41+ <me .panpf.switchbutton.SwitchButton
42+ android : id =" @+id/switch"
43+ android : layout_width =" wrap_content"
44+ android : layout_height =" wrap_content"
45+ android : text =" 接受推送" />
46+ ```
5647
57- ![ switch_slider_normal.png ] ( docs/switch_slider_normal.png )
48+ #### 在代码中使用
5849
59- switch_slider_disable.png
50+ SwitchButton 继承自 CompoundButton ,因此你可以像使用 CheckBox 那样设置状态和监听
6051
61- ![ switch_slider_disable.png ] ( docs/switch_slider_disable.png )
52+ 一些可用的方法:
6253
63- selector_switch_state.xml
54+ * setDrawables(Drawable, Drawable, Drawable, Drawable):设置图片
55+ * setDrawableResIds(int, int, int, int):设置图片ID
56+ * setWithTextInterval(int):设置标题和按钮的间距,默认为 16dp
57+ * setDuration(int):设置动画持续时间,单位毫秒,默认为 200
58+ * setMinChangeDistanceScale(float):设置滑动有效距离比例,默认为 0.2。例如按钮宽度为 100,比例为 0.2,那么只有当滑动距离大于等于 (100* 0.2) 才会切换状态,否则就回滚
6459
65- ``` xml
66- <?xml version =" 1.0" encoding =" utf-8" ?>
67- <selector xmlns : android =" http://schemas.android.com/apk/res/android" >
68- <item android : state_enabled =" false" android : drawable =" @drawable/switch_state_disable" />
69- <item android : drawable =" @drawable/switch_state_normal" />
70- </selector >
71- ```
60+ #### 自定义图片资源
7261
73- selector_switch_slider.xml
62+ SwitchButton 由四张图片构成
7463
75- ``` xml
76- <?xml version =" 1.0" encoding =" utf-8" ?>
77- <selector xmlns : android =" http://schemas.android.com/apk/res/android" >
78- <item android : state_enabled =" false" android : drawable =" @drawable/switch_slider_disable" />
79- <item android : drawable =" @drawable/switch_slider_normal" />
80- </selector >
81- ```
64+ * frameDrawable:框架图片,决定按钮的大小以及显示区域
65+ * stateDrawable:状态图片,显示开启或关闭状态
66+ * stateMaskDrawable:状态图片遮罩层,用于让状态图片按照遮罩层的形状显示
67+ * sliderDrawable:滑块图片
8268
83- #### 在布局中使用 SwitchButton 并通过自定义属性设置图片
69+ 第一种方式你可以通过 SwitchButton 的自定义属性设置资源图片,如下:
8470
8571``` xml
8672<?xml version =" 1.0" encoding =" utf-8" ?>
8773<me .panpf.switchbutton.SwitchButton
88- xmlns : android =" http://schemas.android.com/apk/res/android"
8974 xmlns : app =" http://schemas.android.com/apk/res-auto"
9075 android : id =" @+id/switch"
91- android : layout_width =" match_parent "
76+ android : layout_width =" wrap_content "
9277 android : layout_height =" wrap_content"
93- android : minHeight =" 50dp"
94- android : paddingLeft =" 30dp"
95- android : paddingRight =" 30dp"
9678 android : text =" 接受推送"
97- app : withTextInterval =" 16dp"
9879 app : frameDrawable =" @drawable/switch_frame"
9980 app : stateDrawable =" @drawable/selector_switch_state"
10081 app : stateMaskDrawable =" @drawable/switch_state_mask"
10182 app : sliderDrawable =" @drawable/selector_switch_slider" />
10283```
10384
104- 属性解释:
105- * withTextInterval:标题文字和按钮之间的距离
106- * frameDrawable:框架图片,定义按钮的大小以及显示区域
107- * stateDrawable:状态图片,显示开启或关闭
108- * stateMaskDrawable:状态图片遮罩层
109- * sliderDrawable:滑块图片
110-
111- 其它方法:
112- * setDrawables(Drawable frameBitmap, Drawable stateDrawable, Drawable stateMaskDrawable, Drawable sliderDrawable):设置图片
113- * setDrawableResIds(int frameDrawableResId, int stateDrawableResId, int stateMaskDrawableResId, int sliderDrawableResId):设置图片ID
114- * setWithTextInterval(int withTextInterval):设置标题和按钮的间距,默认为16
115- * setDuration(int duration):设置动画持续时间,单位毫秒,默认为200
116- * setMinChangeDistanceScale(float minChangeDistanceScale):设置滑动有效距离比例,默认为0.2。例如按钮宽度为100,比例为0.2,那么只有当滑动距离大于等于(100* 0.2)才会切换状态,否则就回滚
117-
118- 示例图片源码
119-
120- ``` xml
121- <?xml version =" 1.0" encoding =" utf-8" ?>
122- <ScrollView
123- xmlns:android=" http://schemas.android.com/apk/res/android"
124- xmlns : app =" http://schemas.android.com/apk/res-auto"
125- android:layout_width=" match_parent"
126- android : layout_height =" match_parent"
127- android : background =" @android:color/white" >
128- <LinearLayout
129- android : orientation =" vertical"
130- android : layout_width =" match_parent"
131- android : layout_height =" wrap_content"
132- android : paddingTop =" 16dp"
133- android : paddingBottom =" 16dp" >
134-
135- <me .panpf.switchbutton.SwitchButton
136- style =" @style/item"
137- android : id =" @+id/switch_main_1"
138- android : text =" 接受推送"
139- android : checked =" true"
140- android : background =" @drawable/selector_preference_complete"
141- app : withTextInterval =" 16dp"
142- app : frameDrawable =" @drawable/switch_frame"
143- app : stateDrawable =" @drawable/selector_switch_state"
144- app : stateMaskDrawable =" @drawable/switch_state_mask"
145- app : sliderDrawable =" @drawable/selector_switch_slider" />
146-
147- <me .panpf.switchbutton.SwitchButton
148- style =" @style/item"
149- android : id =" @+id/switch_main_2"
150- android : text =" 每日头条"
151- android : background =" @drawable/selector_preference_header"
152- android : layout_marginTop =" 16dp"
153- app : withTextInterval =" 16dp"
154- app : frameDrawable =" @drawable/switch_frame"
155- app : stateDrawable =" @drawable/selector_switch_state"
156- app : stateMaskDrawable =" @drawable/switch_state_mask"
157- app : sliderDrawable =" @drawable/selector_switch_slider" />
158-
159- <me .panpf.switchbutton.SwitchButton
160- style =" @style/item"
161- android : id =" @+id/switch_main_3"
162- android : text =" 每日热卖"
163- android : background =" @drawable/selector_preference_center"
164- app : withTextInterval =" 16dp"
165- app : frameDrawable =" @drawable/switch_frame"
166- app : stateDrawable =" @drawable/selector_switch_state"
167- app : stateMaskDrawable =" @drawable/switch_state_mask"
168- app : sliderDrawable =" @drawable/selector_switch_slider" />
169-
170- <me .panpf.switchbutton.SwitchButton
171- style =" @style/item"
172- android : id =" @+id/switch_main_4"
173- android : text =" 优惠促销"
174- android : background =" @drawable/selector_preference_center"
175- app : withTextInterval =" 16dp"
176- app : frameDrawable =" @drawable/switch_frame"
177- app : stateDrawable =" @drawable/selector_switch_state"
178- app : stateMaskDrawable =" @drawable/switch_state_mask"
179- app : sliderDrawable =" @drawable/selector_switch_slider" />
180-
181- <me .panpf.switchbutton.SwitchButton
182- style =" @style/item"
183- android : id =" @+id/switch_main_5"
184- android : text =" 偶像动态"
185- android : background =" @drawable/selector_preference_center"
186- app : withTextInterval =" 16dp"
187- app : frameDrawable =" @drawable/switch_frame"
188- app : stateDrawable =" @drawable/selector_switch_state"
189- app : stateMaskDrawable =" @drawable/switch_state_mask"
190- app : sliderDrawable =" @drawable/selector_switch_slider" />
191-
192- <me .panpf.switchbutton.SwitchButton
193- style=" @style/item"
194- android:id=" @+id/switch_main_6"
195- android:text=" 娱乐播报"
196- android:background=" @drawable/selector_preference_footer"
197- app:withTextInterval=" 16dp"
198- app:frameDrawable=" @drawable/switch_frame"
199- app:stateDrawable=" @drawable/selector_switch_state"
200- app:stateMaskDrawable=" @drawable/switch_state_mask"
201- app:sliderDrawable=" @drawable/selector_switch_slider" />
202-
203- <me .panpf.switchbutton.SwitchButton
204- style =" @style/item"
205- android : id =" @+id/switch_main_7"
206- android : text =" 天气预报"
207- android : background =" @drawable/selector_preference_header"
208- android : layout_marginTop =" 16dp"
209- app : withTextInterval =" 16dp"
210- app : frameDrawable =" @drawable/switch_frame"
211- app : stateDrawable =" @drawable/selector_switch_state"
212- app : stateMaskDrawable =" @drawable/switch_state_mask"
213- app : sliderDrawable =" @drawable/selector_switch_slider" />
214-
215- <me .panpf.switchbutton.SwitchButton
216- style =" @style/item"
217- android : id =" @+id/switch_main_8"
218- android : text =" 系统消息"
219- android : background =" @drawable/selector_preference_footer"
220- app : withTextInterval =" 16dp"
221- app : frameDrawable =" @drawable/switch_frame"
222- app : stateDrawable =" @drawable/selector_switch_state"
223- app : stateMaskDrawable =" @drawable/switch_state_mask"
224- app : sliderDrawable =" @drawable/selector_switch_slider" />
225- </LinearLayout >
226- </ScrollView >
227- ```
85+ 第二种方式是通过上面提到的 setDrawables(Drawable, Drawable, Drawable, Drawable) 或 setDrawableResIds(int, int, int, int) 方法设置资源图片
22886
22987### License
23088
0 commit comments