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

相关推荐
alexhilton2 分钟前
在Android应用中实战Repository模式
android·kotlin·android jetpack
二流小码农4 小时前
鸿蒙开发:DevEcoTesting中的稳定性测试
android·ios·harmonyos
一起搞IT吧5 小时前
相机Camera日志实例分析之二:相机Camx【专业模式开启直方图拍照】单帧流程日志详解
android·图像处理·数码相机
xzkyd outpaper5 小时前
Android中ContentProvider细节
android·计算机八股
恋猫de小郭5 小时前
Flutter 多版本管理工具 Puro ,它和 FVM 有什么区别?
android·前端·flutter
newki5 小时前
学习笔记,关于NDK/JNI的简介与实战
android·c++·app
zhangphil6 小时前
Android屏幕刷新率与FPS(Frames Per Second) 120hz
android
江湖有缘6 小时前
华为云Flexus+DeepSeek征文| 华为云Flexus X实例单机部署Dify-LLM应用开发平台全流程指南
android·华为云·rxjava
一杯凉白开6 小时前
硬件工程师口中的取低八位,中八位,高八位是什么意思?
android·网络协议
番茄憨憨6 小时前
Android-wifi常用接口,9个里面你看看几个对你有帮助!
android