Android 两个view并列显示

两个view并列显示,第二个view跟在第一个view的后面,如果长度过长,则第一个view宽度缩小。

直接上代码  用就完了!

public class TextWithImageLayout extends LinearLayout {

    private final int MARGIN = 40;
    private int mMargin;

    private int[] mChildWidthList;

    public TextWithImageLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mMargin = MARGIN;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int count = getChildCount();
        View firstChild = getChildAt(0);

        if (count > 1) {
            int firstChildWidth = firstChild.getMeasuredWidth();
            int firstChildHeight = firstChild.getMeasuredHeight();
            int otherChildrenWidth = 0;
            int otherChildrenHeight = 0;

            for (int i = 1; i < count; i++) {
                View child = getChildAt(i);
                int width;
                if (mChildWidthList == null) {
                    width = child.getMeasuredWidth();
                } else {
                    width = mChildWidthList[i - 1];
                }
                otherChildrenWidth += width;
                child.measure(width | MeasureSpec.EXACTLY, child.getMeasuredHeight() | MeasureSpec.EXACTLY);
                otherChildrenHeight = child.getMeasuredHeight();
            }

            int margin = mMargin * (count - 1);
            if (firstChildWidth + otherChildrenWidth + margin > widthSize) {
                firstChildWidth = widthSize - otherChildrenWidth - margin;
            }

            heightSize = firstChildHeight > otherChildrenHeight ? firstChildHeight : otherChildrenHeight;

            firstChild.measure(firstChildWidth | MeasureSpec.EXACTLY,
                    firstChildHeight | MeasureSpec.EXACTLY);
        } else {
            firstChild.measure(firstChild.getMeasuredWidth(), firstChild.getMeasuredHeight());
            heightSize = firstChild.getMeasuredHeight();
        }

        setMeasuredDimension(widthSize, heightSize);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        int count = getChildCount();

        int left = 0;
        for (int i = 0; i < count; i++) {

            final View child = this.getChildAt(i);
            int width = child.getMeasuredWidth();
            int height = child.getMeasuredHeight();
            int top = (b - t) / 2 - height / 2;

            child.layout(left, top, left + width, top + height);

            left = left + width + mMargin;
        }

    }

    public int[] getChildWidthList() {
        return mChildWidthList;
    }

    public void setChildWidthList(int[] childWidthList) {
        this.mChildWidthList = childWidthList;
    }
}

 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值