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
原创粉丝点击