两种方式实现Textview动态MaxWidth

背景&目标

如上图

  1. 描述:父布局为蓝色框,紫色为长度可变的TV1, 绿色为长度可变的TV2

  2. 目标是:当TV1 + TV2的长度,超过父布局的长度时,先显示完整的TV2,再看剩下的空间是否可以显示TV1, 如不能完整显示TV1, 则添加省略号...

方案一

使用LinearLayout 的 RTL 布局 ,android:gravity="end"

Kotlin 复制代码
<LinearLayout
    android:layout_marginTop="600dp"
    android:background="@color/button_pressed"
    android:layout_width="200dp"
    android:orientation="horizontal"
    android:layoutDirection="rtl"
    android:gravity="end"
    android:layout_height="50dp">
    <TextView
        android:text="朋友"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"/>
    <TextView
        android:text="测试测试测试测试测试测试测试测试测试"
        android:gravity="center"
        android:maxLines="1"
        android:ellipsize="end"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"/>


</LinearLayout>

这个方案在某些不支持rtl布局的app(supportsRtl = "false")中是不能用的。所以可以使用下面的方案二

方案二

使用

  • ConstraintLayout

  • app:layout_constrainedWidth="true"

  • app:layout_constraintHorizontal_chainStyle="packed"

  • app:layout_constraintHorizontal_bias="0"

Kotlin 复制代码
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_marginTop="650dp"
    android:id="@+id/content"
    android:layout_width="200dp"
    android:layout_height="50dp"
    android:background="@color/button_pressed">
    <TextView
        android:id="@+id/tv2"
        android:textSize="16sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="4dp"
        android:ellipsize="end"
        android:singleLine="true"
        android:textColor="#99161823"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/tv1"
        app:layout_constraintTop_toTopOf="@id/tv1"
        app:layout_constraintBottom_toBottomOf="@id/tv1"
        android:text="朋友" />

    <TextView
        android:gravity="center"
        android:id="@+id/tv1"
        android:textSize="16sp"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:ellipsize="end"
        android:maxLines="1"
        app:layout_constrainedWidth="true"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/tv2"
        app:layout_constraintHorizontal_chainStyle="packed"
        tools:text="测试~测试~测试~测试~测试~测试~测试~" />
</androidx.constraintlayout.widget.ConstraintLayout>

关键属性已经标注

  • app:layout_constraintHorizontal_chainStyle="packed"作用是让tv1,tv2居中

  • app:layout_constraintHorizontal_bias="0" 作用是让packed作用后,居左, 因为要求是视图不能居中

  • app:layout_constrainedWidth="true" 作用让tv1在ConstraintLayout中受到宽度约束,不越界。当Tv1宽度越界后,用省略号替换

相关推荐
城东米粉儿6 小时前
关于ObjectAnimator
android
zhangphil7 小时前
Android渲染线程Render Thread的RenderNode与DisplayList,引用Bitmap及Open GL纹理上传GPU
android
火柴就是我8 小时前
从头写一个自己的app
android·前端·flutter
lichong9519 小时前
XLog debug 开启打印日志,release 关闭打印日志
android·java·前端
用户693717500138410 小时前
14.Kotlin 类:类的形态(一):抽象类 (Abstract Class)
android·后端·kotlin
火柴就是我10 小时前
NekoBoxForAndroid 编译libcore.aar
android
Kaede611 小时前
MySQL中如何使用命令行修改root密码
android·mysql·adb
明君8799712 小时前
Flutter 图纸标注功能的实现:踩坑与架构设计
android·ios
成都大菠萝12 小时前
Android Auto开发(3)-Audio Integration
android
成都大菠萝12 小时前
Android Auto开发(5)-Audio Integration
android