Android SegmentView
来源:互联网 发布:上海软件外包公司 编辑:程序博客网 时间:2024/05/16 08:42
Android Segment
在IOS开发中有一个很好用的控件SegmentControl,而在Android要实现相同的功能就需要自己手动完成,如果能有一个已经封装好了的功能类似的控件就好了,基于这个想法,自己实现了一个Android版的SegmentView。
代码比较简单,就是在一个LinearLayout下动态的生成各个Segment Item,通过RoundRectShape画好圆角矩阵背景。最左边和右边的Item的背景需要特殊处理(有圆角),具体代码如下(最左边的,最右边的和左边的类似):
int selected = android.R.attr.state_selected; float[] radiusBuffer = new float[]{mRadius, mRadius, 0, 0, 0, 0, mRadius, mRadius}; RoundRectShape shape = new RoundRectShape(radiusBuffer, null, null); ShapeDrawable selectedDrawable = new ShapeDrawable(shape); selectedDrawable.getPaint().setAntiAlias(true); selectedDrawable.getPaint().setColor(mSelectedColor); ShapeDrawable normalDrawable = new ShapeDrawable(shape); normalDrawable.getPaint().setAntiAlias(true); normalDrawable.getPaint().setColor(mColor); mLeftDrawable = new StateListDrawable(); mLeftDrawable.addState(new int[]{selected}, selectedDrawable); mLeftDrawable.addState(new int[]{-selected}, normalDrawable);
其中的radiusBuffer是用来设定圆角矩形的各个角的半径的。
public class Item { private View mCustomView; private ItemView mItemView; private Context mContext; private ColorStateList mTextColorList; private void initColorList() { int selected = android.R.attr.state_selected; mTextColorList = new ColorStateList(new int[][]{{selected}, {-selected}}, new int[]{mSelectedTextColor, mTextColor}); } private LayoutParams newLayoutParams() { LayoutParams lp = new LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT); lp.weight = 1; return lp; } private void checkItemView() { if (mItemView == null) { mItemView = new ItemView(mContext); if (mTextColorList == null) { initColorList(); } mItemView.mTextView.setTextColor(mTextColorList); mItemView.mTextView.setTextSize(mTextSize); mItemView.setLayoutParams(newLayoutParams()); } } public Item(Context context) { mContext = context; } public Item setCustomView(View customView) { mCustomView = customView; mCustomView.setLayoutParams(newLayoutParams()); return this; } public Item setIcon(int drawableId) { checkItemView(); mItemView.mIconView.setImageResource(drawableId); return this; } public Item setText(int strId) { checkItemView(); mItemView.mTextView.setText(strId); return this; } public Item setText(String str) { checkItemView(); mItemView.mTextView.setText(str); return this; } }
Item表示将要添加到Segment上的元素,其中ItemView是默认的元素View而CustomView则是用户自定义的View元素。如果设置了CustomView则ItemView不会起作用,而ItemView也是惰性初始化的,如果设置CustomView,那么ItemView并不会占用空间。
整体代码就不上了,如果有需要可以到git上去下载
https://github.com/belows/SegmentDemo
0 0
- Android SegmentView
- Android 访IOS 分段选择控制器 SegmentView
- 自定义segmentView
- 基于系统的封装SegmentView类
- Android
- android
- Android
- android
- android
- Android
- Android
- android
- android
- android
- Android
- Android
- android!!!
- android
- 用haar特征训练自己的分类器
- 什么是程序设计语言
- rxlint: 检查RxJava代码的Android lint规则
- 【笔记】 《js权威指南》- 第9章 类和模块 - 9.9 模块
- tomcat 运行一段时间后报错 compacting perm gen
- Android SegmentView
- Kalman滤波做运动目标跟踪
- Oracle普通表转时间类型自动分区表
- 在写一个iOS应用之前必须做的7件事
- 排序算法之堆排序
- 英文自动换行 and 中文超出隐藏
- 进程和线程,堆和栈(整理)
- iOS进阶推荐书目,网站以及链接
- 基础函数宏