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

相关推荐
走在路上的菜鸟32 分钟前
Android学Dart学习笔记第二十一节 类-点的简写
android·笔记·学习·flutter
QQ12958455041 小时前
ThingsBoard-修改Android APP应用程序名和描述
android·物联网·iot
代码代码快快显灵1 小时前
Android项目架构深度解析
android
丐中丐9992 小时前
一个Binder通信中的多线程同步问题
android
诸神黄昏EX2 小时前
Android Qualcomm USB 专题系列【篇二:UsbGadget模式配置】
android
vocal2 小时前
【我的AOSP第一课】Android Init 语言与 rc 文件
android
诸神黄昏EX2 小时前
Android Qualcomm USB 专题系列【总篇:USB HAL架构】
android·linux·网络
原神启动12 小时前
Ansible(三)—— 使用Ansible自动化部署LNMP环境
android·自动化·ansible
前端老白3 小时前
webview在微信小程序中,安卓加载失败,IOS正常加载
android·ios·微信小程序·webview
2501_937154933 小时前
适配中兴主流机型 纯净版刷机固件技术优势合集
android·源码·源代码管理·机顶盒