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

相关推荐
饭小猿人1 天前
Android 腾讯X5WebView如何禁止系统自带剪切板和自定义剪切板视图
android·java
_李小白1 天前
【android opencv学习笔记】Day 8: remap(像素位置重映射)
android·opencv·学习
美狐美颜SDK开放平台1 天前
多场景美颜SDK解决方案:直播APP(iOS/安卓)开发接入详解
android·人工智能·ios·音视频·美颜sdk·第三方美颜sdk·短视频美颜sdk
嗷o嗷o1 天前
Android BLE 里,MTU、分包和长数据发送到底该怎么处理
android
Gary Studio1 天前
Android AIDL HAL工程结构示例
android
y = xⁿ1 天前
MySQL八股知识合集
android·mysql·adb
andr_gale1 天前
04_rc文件语法规则
android·framework·aosp
祖国的好青年1 天前
VS Code 搭建 React Native 开发环境(Windows 实战指南)
android·windows·react native·react.js
黄林晴1 天前
警惕!AGP 9.2 别只改版本号,R8 规则与构建链路全线收紧
android·gradle