两种方式实现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宽度越界后,用省略号替换

相关推荐
louisgeek3 分钟前
Android NSD 网络服务发现
android
张可1 小时前
历时两年半开发,Fread 项目现在决定开源,基于 Kotlin Multiplatform 和 Compose Multiplatform 实现
android·前端·kotlin
余辉zmh1 小时前
【Linux系统篇】:信号的生命周期---从触发到保存与捕捉的底层逻辑
android·java·linux
孤鸿玉2 小时前
[Flutter小试牛刀] 低配版signals,添加多层监听链
android·前端·响应式设计
雨和卡布奇诺2 小时前
LiveData源码浅析
android
淡蓝色_justin2 小时前
Hilt-plus 简介
android·android jetpack
app1e2342 小时前
ctfshow web入门 命令执行(29-77)
android·前端
恋猫de小郭4 小时前
Flutter 在 Dart 3.8 开始支持 Null-Aware Elements 语法,自动识别集合里的空元素
android·前端·flutter
fatiaozhang95275 小时前
咪咕MG101_晨星MSO9380芯片_安卓5.1.1_免拆卡刷固件包
android·电视盒子·av1·机顶盒rom·魔百盒刷机