Skip to content

Commit 4f2b89d

Browse files
authored
Merge pull request zcweng#29 from robertoallende/feature/buttonColors
Feature/buttonColors
2 parents 8a20351 + ed07b62 commit 4f2b89d

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

library/src/main/java/com/suke/widget/SwitchButton.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ private void init(Context context, AttributeSet attrs) {
135135
int buttonColor = optColor(typedArray,
136136
R.styleable.SwitchButton_sb_button_color,
137137
Color.WHITE);//Color.WHITE;
138+
139+
uncheckButtonColor = optColor(typedArray,
140+
R.styleable.SwitchButton_sb_uncheckbutton_color,
141+
buttonColor);
142+
143+
checkedButtonColor = optColor(typedArray,
144+
R.styleable.SwitchButton_sb_checkedbutton_color,
145+
buttonColor);
138146

139147
int effectDuration = optInt(typedArray,
140148
R.styleable.SwitchButton_sb_effect_duration,
@@ -251,6 +259,7 @@ private void setUncheckViewState(ViewState viewState){
251259
viewState.checkStateColor = uncheckColor;
252260
viewState.checkedLineColor = Color.TRANSPARENT;
253261
viewState.buttonX = buttonMinX;
262+
buttonPaint.setColor(uncheckButtonColor);
254263
}
255264

256265
/**
@@ -261,6 +270,7 @@ private void setCheckedViewState(ViewState viewState){
261270
viewState.checkStateColor = checkedColor;
262271
viewState.checkedLineColor = checkLineColor;
263272
viewState.buttonX = buttonMaxX;
273+
buttonPaint.setColor(checkedButtonColor);
264274
}
265275

266276
@Override
@@ -903,8 +913,16 @@ private static boolean optBoolean(TypedArray typedArray,
903913
*打开指示线位移Y
904914
*/
905915
private float checkedLineOffsetY;
906-
907-
916+
/**
917+
* Color for button when it's uncheck
918+
*/
919+
private int uncheckButtonColor;
920+
/**
921+
* Color for button when it's check
922+
*/
923+
private int checkedButtonColor;
924+
925+
908926
/**
909927
* 按钮最左边
910928
*/

library/src/main/res/values/switch_button_attrs.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,9 @@
2121
<attr name="sb_show_indicator" format="reference|boolean"/>
2222
<attr name="sb_background" format="reference|color"/>
2323
<attr name="sb_enable_effect" format="reference|boolean"/>
24+
25+
<attr name="sb_checkedbutton_color" format="reference|color"/>
26+
<attr name="sb_uncheckbutton_color" format="reference|color"/>
27+
2428
</declare-styleable>
2529
</resources>

sample/src/main/res/layout/activity_main.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
app:sb_checkline_color="#a5dc88"
4444
app:sb_checked_color="#A36F95"
4545
app:sb_uncheckcircle_color="#A36F95"
46+
app:sb_uncheckbutton_color="#fdc951"
47+
app:sb_checkedbutton_color="#61d74f"
4648
/>
4749

4850
<com.suke.widget.SwitchButton

0 commit comments

Comments
 (0)