Linearlayout横向布局两个textview,优先显示后面textview,前面textview省略显示

在项目中有事需要在Linearlayout横向布局中有两个textview情况下,优先后面view完全显示,当不能完全显示是前面的textview省略显示,但android自带的Linearlayout不能满足需求,总是优先显示前面的view,所以针对这种情况对Linearlayout做了自定义封装
重写onMeasure方法,重新测量子view控制优先显示后面的子view

自定义Linearlayout

public class XLinearlayout extends LinearLayout {

    private int rest;
    int mLeft, mRight, mTop, mBottom;

    public XLinearlayout(Context context) {
        this(context, null);
    }


    public XLinearlayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //非横排和不开启RearFirst,则用系统默认measure
        if (!isHorizon()) {
            return;
        }
        int mWidth  = MeasureSpec.getSize(widthMeasureSpec);//可用宽度
        int mHeight = getMeasuredHeight();
        int mCount  = getChildCount();//子view数量
        //计算预计总宽
        int preComputeWidth = 0;
        //临时记录位置
        mLeft = 0;
        mRight = 0;
        mTop = 0;
        mBottom = 0;

        rest = mWidth;

        for (int i = mCount - 1; i >= 0; i--) {//从后往前算(因为要顺便计算position以备使用)
            final View child = getChildAt(i);
            int        spec  = MeasureSpec.makeMeasureSpec(rest, MeasureSpec.AT_MOST);
            child.measure(spec, MeasureSpec.UNSPECIFIED);
            int childw = child.getMeasuredWidth();
            int childh = child.getMeasuredHeight();
            preComputeWidth += childw;

            //计算rest
            mRight = getPositionRight(i, mCount, mWidth);
            mLeft = mRight - childw;
            mBottom = mTop + childh;
            rest = mLeft;
        }
        //保存最终的测量结果
        if (preComputeWidth <= mWidth) {
            setMeasuredDimension(preComputeWidth, mHeight);
        } else {
            setMeasuredDimension(mWidth, mHeight);
        }
    }

    private boolean isHorizon() {
        return getOrientation() == HORIZONTAL;
    }

    public int getPositionRight(int index, int count, int totalWidth) {
        if (index < count - 1) {
            return getPositionRight(index + 1, count, totalWidth) - getChildAt(index + 1).getMeasuredWidth();
        }
        return totalWidth - getPaddingRight();
 

布局中调用

<net.liexiang.dianjing.widget.XLinearlayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/tv_textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:lines="1"
        android:text="迷糊一二三四五六七八九十"
        android:textSize="16sp" />
    <TextView
        android:id="@+id/tv_textview2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:lines="1"
        android:text="一二三四五六七八九十1234567890"
        android:textSize="13sp" />
</net.liexiang.dianjing.widget.XLinearlayout>
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值