[关闭]
@linux1s1s 2017-01-22T08:15:57.000000Z 字数 3595 阅读 1840

仿IOS可滑动开关自定义样式

AndroidWidget 2015-11


关于这个话题,网上有不少第三方Demo,这里参考 SwitchButton 开关按钮 的多种实现方式 ,为了使其更易于使用,我们稍作修改,大体的修改思路是增加xml自定义样式,使得View分离的更为清晰明了。

定义styleable

  1. <resources>
  2. <declare-styleable name="customCheckSwitchButton">
  3. <attr name="checkswitch_bottom" format="reference|color" />
  4. <attr name="checkswitch_btn_pressed" format="reference|color" />
  5. <attr name="checkswitch_btn_unpressed" format="reference|color" />
  6. <attr name="checkswitch_frame" format="reference|color" />
  7. <attr name="checkswitch_mask" format="reference|color" />
  8. </declare-styleable>
  9. </resources>

改造View构造器

  1. public CheckSwitchButton(Context context, AttributeSet attrs) {
  2. this(context, attrs, android.R.attr.checkboxStyle);
  3. }
  4. public CheckSwitchButton(Context context, AttributeSet attrs, int defStyle) {
  5. super(context, attrs, defStyle);
  6. final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.customCheckSwitchButton);
  7. final int N = a.getIndexCount();
  8. for (int i = 0; i < N; i++) {
  9. int attr = a.getIndex(i);
  10. switch (attr) {
  11. case R.styleable.customCheckSwitchButton_checkswitch_bottom:
  12. Drawable bottomD = a.getDrawable(attr);
  13. if (bottomD != null) {
  14. BitmapDrawable bottomBD = (BitmapDrawable) bottomD;
  15. if (bottomBD != null) {
  16. mBottom = bottomBD.getBitmap();
  17. }
  18. }
  19. break;
  20. case R.styleable.customCheckSwitchButton_checkswitch_btn_pressed:
  21. Drawable pressedD = a.getDrawable(attr);
  22. if (pressedD != null) {
  23. BitmapDrawable bottomBD = (BitmapDrawable) pressedD;
  24. if (bottomBD != null) {
  25. mBtnPressed = bottomBD.getBitmap();
  26. }
  27. }
  28. break;
  29. case R.styleable.customCheckSwitchButton_checkswitch_btn_unpressed:
  30. Drawable unPressedD = a.getDrawable(attr);
  31. if (unPressedD != null) {
  32. BitmapDrawable bottomBD = (BitmapDrawable) unPressedD;
  33. if (bottomBD != null) {
  34. mBtnNormal = bottomBD.getBitmap();
  35. }
  36. }
  37. break;
  38. case R.styleable.customCheckSwitchButton_checkswitch_frame:
  39. Drawable frameD = a.getDrawable(attr);
  40. if (frameD != null) {
  41. BitmapDrawable bottomBD = (BitmapDrawable) frameD;
  42. if (bottomBD != null) {
  43. mFrame = bottomBD.getBitmap();
  44. }
  45. }
  46. break;
  47. case R.styleable.customCheckSwitchButton_checkswitch_mask:
  48. Drawable maskD = a.getDrawable(attr);
  49. if (maskD != null) {
  50. BitmapDrawable bottomBD = (BitmapDrawable) maskD;
  51. if (bottomBD != null) {
  52. mMask = bottomBD.getBitmap();
  53. }
  54. }
  55. break;
  56. }
  57. }
  58. a.recycle();
  59. final Resources resources = context.getResources();
  60. //Default
  61. if (mBottom == null) {
  62. mBottom = BitmapFactory.decodeResource(resources, R.drawable.checkswitch_bottom);
  63. }
  64. if (mBtnPressed == null) {
  65. mBtnPressed = BitmapFactory.decodeResource(resources, R.drawable.checkswitch_btn_pressed);
  66. }
  67. if (mBtnNormal == null) {
  68. mBtnNormal = BitmapFactory.decodeResource(resources, R.drawable.checkswitch_btn_unpressed);
  69. }
  70. if (mFrame == null) {
  71. mFrame = BitmapFactory.decodeResource(resources, R.drawable.checkswitch_frame);
  72. }
  73. if (mMask == null) {
  74. mMask = BitmapFactory.decodeResource(resources, R.drawable.checkswitch_mask);
  75. }
  76. initView(context);
  77. }

主要的修改在这个View构造器中实现,从XML中解析各种样式并初始化,L65行初始化默认样式,接下来就是xml的简单自定义了。

xml自定义样式

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:custom="http://schemas.android.com/apk/res-auto"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. android:padding="5dip" >
  7. <com.example.compoundbuttonview.view.CheckSwitchButton
  8. android:id="@+id/checkSwithcButton"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_gravity="center"
  12. android:enabled="true"
  13. custom:checkswitch_bottom="@drawable/checkswitch_bottom"
  14. custom:checkswitch_btn_pressed="@drawable/checkswitch_btn_pressed"
  15. custom:checkswitch_btn_unpressed="@drawable/checkswitch_btn_unpressed"
  16. custom:checkswitch_frame="@drawable/checkswitch_frame"
  17. custom:checkswitch_mask="@drawable/checkswitch_mask" />
  18. </LinearLayout>

使用

接下来就可以直接拿来主义了

  1. mCheckSwithcButton = (CheckSwitchButton)findViewById(R.id.checkSwithcButton);

最后看一下效果图
此处输入图片的描述

改造结束,虽然比较简单,却使得这个控件非常易于使用和调配。最后,感谢一下原作者的无私奉献。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注