【Android UI】自定义Switch(无滑动效果)
来源:互联网 发布:商务部 融资租赁数据 编辑:程序博客网 时间:2024/05/16 07:26
1、定义Switch类,继承CompoundButton
public class BlueSwitch extends CompoundButton { private Drawable switchOn; private Drawable switchOff; private boolean mChecked; private OnCheckedChangeListener mOnCheckedChangeListener; public interface OnCheckedChangeListener { public void onCheckedChanged(BlueSwitch switchView, boolean isChecked); } public BlueSwitch(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.BlueSwitch, 0, 0); try { switchOn = a.getDrawable(R.styleable.BlueSwitch_switchOn); switchOff = a.getDrawable(R.styleable.BlueSwitch_switchOff); }catch (Exception e) { e.printStackTrace(); } a.recycle(); } public BlueSwitch(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public BlueSwitch(Context context) { super(context); } @Override public void setChecked(boolean checked) { super.setChecked(checked); mChecked = checked; udpateDrawable(); } private void udpateDrawable() { if(mChecked) { //Log.e(BlueSwitch.class.getName(), "changing to switchOn"); this.setBackground(switchOn); }else { //Log.e(BlueSwitch.class.getName(),"changing to switchOff"); this.setBackground(switchOff); } } @Override public boolean isChecked() { return mChecked; } @Override protected void onFinishInflate() { udpateDrawable(); super.onFinishInflate(); } @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); switch (action) { case MotionEvent.ACTION_UP: setChecked(!mChecked); if (mOnCheckedChangeListener != null) { mOnCheckedChangeListener.onCheckedChanged(this, mChecked); } break; default: // Do nothing break; } return true; } public void setOnCheckedChangeListener(OnCheckedChangeListener listener) { mOnCheckedChangeListener = listener; }}
2、为switch添加自定义属性
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="BlueSwitch"> <attr name="switchOn" format="reference"/> <attr name="switchOff" format="reference"/> </declare-styleable></resources>
3、使用
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.gigaset.cloudphoto.widgets.BlueSwitch android:layout_width="45dp" android:layout_height="27dp" app:switchOn="@drawable/switch_on" app:switchOff="@drawable/switch_off"/></LinearLayout>
4、图片附件
switch_on.png
switch_off.png
5、带滑动效果的switch组件,参考:
https://github.com/kyleduo/SwitchButton
0 0
- 【Android UI】自定义Switch(无滑动效果)
- Android 自定义简单的滑动效果switch开关
- 【Android】自定义控件实现可滑动的开关(switch)
- 自定义Switch滑动开关
- Android UI效果实现——Activity滑动退出效果
- Android UI效果实现 滑动模糊渐变效果实现
- Android-自定义滑动菜单(抽屉效果)
- Android 自定义 HorizontalScrollView 横向滑动效果
- android 设置自定义dialog大小无效果
- Android UI - 带白边的自定义ImageView效果
- Android UI(Switch)详解
- Android UI Switch控件
- 【Android UI设计与开发】10:滑动菜单栏(二)SlidingMenu 动画效果的实现
- Android UI设计(引导界面):ViewPager之二滑动切换效果
- android ui listview 下拉刷新,上滑动刷新效果
- 【Android UI】案例03滑动切换效果的实现(ViewPager)
- Android UI, GridView, ScrollView, SlidingDrawer 抽屉滑动效果
- Android 自定义 UI-----跟随手指滑动的小球
- 南阳oj 组合数【搜索 字典序】
- OC-省市区数组
- 对HBase整个框架的理解
- hdu1879 继续畅通工程(最小生成树)
- hive建表等操作
- 【Android UI】自定义Switch(无滑动效果)
- Add Two Numbers
- HDOJ 1312 Red and Black (简单dfs)
- leetcode刷题,总结,记录,备忘154
- 来京学习第10天
- ST算法(RMQ模板)
- C语言宏定义,内置宏,__FILE__,__LINE__,## 用法
- SHA-256算法 C语言实现
- 夜间肌肤护肤要做到6点 轻松摆脱各种肌肤问题