自定义View 实现横向的搜索记录

 author:Created by WeiWeiFeng on 2018/4/14.
 */

public class HorizontalView extends ViewGroup {
    int mLeft=20;
    int mTop=10;
    public HorizontalView(Context context) {
        this(context,null);
    }

    public HorizontalView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public HorizontalView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        int viewWidth=0;
        int viewHeigth=0;
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);

        int modeHeigth = MeasureSpec.getMode(heightMeasureSpec);
        int sizeHeigth = MeasureSpec.getSize(heightMeasureSpec);
            switch(modeWidth){
                    case MeasureSpec.AT_MOST:
                    //自适应
                        for (int i = 0; i < getChildCount(); i++) {
                            View childAt = getChildAt(i);
                            int measuredWidth1 = childAt.getMeasuredWidth();
                            viewWidth+=measuredWidth1+mLeft;
                        }
                        viewHeigth+=mLeft*2-10;
                    break;
                    case MeasureSpec.EXACTLY:
                        viewWidth=sizeWidth;
                    break;
                }
                switch(modeHeigth){
                        case MeasureSpec.AT_MOST:
                            View childAt = getChildAt(0);
                            int childAtMeasuredHeight = childAt.getMeasuredHeight();
                            viewHeigth=childAtMeasuredHeight+mTop*2;
                            break;
                        case MeasureSpec.EXACTLY:
                        viewHeigth=sizeHeigth;
                        break;

                    }

        setMeasuredDimension(viewWidth,viewHeigth);
    }

    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        int left=mLeft;
        int top=mTop;
        int maginLeft=10;
        for (int j = 0; j < getChildCount(); j++) {
            View childAt = getChildAt(j);
            childAt.layout(left,top,childAt.getMeasuredWidth()+left,childAt.getMeasuredHeight()+top);
            left+=childAt.getMeasuredWidth()+maginLeft;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值