美文网首页Android
两个TextView在同一行优先省略前者

两个TextView在同一行优先省略前者

作者: 合拍v | 来源:发表于2020-12-23 15:42 被阅读0次

      最近写到一个东西,一行文字里有两个TextView,只有一行,当文字超出时先省略前面的TextView。貌似iOS可以直接设置一个属性,Android中一时找不到设置哪些属性实现这个效果,不得已自定义了一个Layout,直接继承了LinearLayout,需要改的东西并不多。

    public class EllipsisOptionalLayout extends LinearLayout {
    
      public EllipsisOptionalLayout(Context context) {
        super(context);
      }
    
      public EllipsisOptionalLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
      }
    
      public EllipsisOptionalLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
      }
    
      /**
       * 重写onMeasure函数
       */
      @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // 只对两个TextView生效,其他没有尝试
        if (getChildCount() > 1) {
          View rightChildView = getChildAt(1);
          View leftChildView = getChildAt(0);
          int leftMargin = 0;
          int rightMargin = 0;
          // 计算两个子View的margin大小
          try {
            rightMargin = ((MarginLayoutParams) rightChildView.getLayoutParams()).leftMargin
                + ((MarginLayoutParams) rightChildView.getLayoutParams()).rightMargin;
            leftMargin = ((MarginLayoutParams) leftChildView.getLayoutParams()).leftMargin
                + ((MarginLayoutParams) leftChildView.getLayoutParams()).rightMargin;
          } catch (Exception e) {
            Log.e("EllipsisOptionalLayout", "cast failed");
          }
          // 计算右边View的宽度
          int rightChildWidth = rightChildView.getMeasuredWidth()
              + rightChildView.getPaddingLeft()
              + rightChildView.getPaddingRight() + rightMargin;
          getChildAt(1).measure(
              MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.AT_MOST),
              heightMeasureSpec);
          int rightChildMaxWidth = rightChildView.getMeasuredWidth()
              + rightMargin;
          int selfWidth = getMeasuredWidth();
          if (rightChildWidth < rightChildMaxWidth) {
            // 留给左边View的宽度
            int allowLeftChildWidth = selfWidth
                - rightChildMaxWidth
                - leftMargin
                - leftChildView.getPaddingLeft()
                - leftChildView.getPaddingRight();
            getChildAt(0).measure(MeasureSpec.makeMeasureSpec(allowLeftChildWidth, MeasureSpec.AT_MOST),
                heightMeasureSpec);
          }
        }
      }
    }
    

    使用起来和LinearLayout一样,加上背景色看看效果

    <com.k.demo.EllipsisOptionalLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#96CDCD"
        android:orientation="horizontal">
    
        <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:ellipsize="end"
          android:maxLines="1"
          android:text=""
          android:textColor="#222222"
          android:background="#FFEFD5"
          android:textSize="15sp"
          android:textStyle="bold" />
    
        <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:maxLines="1"
          android:text=""
          android:textColor="#222222"
          android:background="#BBFFFF"
          android:textSize="15sp" />
      </com.k.demo.EllipsisOptionalLayout>
    

    文字长度未超出时:


    image.png

    长度超出时:


    image.png

    nice!
    如有不足,欢迎指正。

    相关文章

      网友评论

        本文标题:两个TextView在同一行优先省略前者

        本文链接:https://www.haomeiwen.com/subject/wmwngktx.html