Android ConstraintLayout layout_constrainedWidth使用

问题

有个需求,在一行中显示收件人姓名和电话,分别显示在行的两端,如下图所示

布局代码如下

XML 复制代码
        <TextView
            android:id="@+id/contact"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:clickable="false"
            android:ellipsize="end"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:maxLines="1"
            android:text='收货人:李想'
            android:textColor="@color/black1"
            android:textSize="14dp"
            app:layout_constraintBottom_toTopOf="@id/address"
            app:layout_constraintStart_toEndOf="@id/tv_first"
            app:layout_constraintEnd_toStartOf="@id/phone"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintHorizontal_chainStyle="spread_inside"/>
        <TextView
            android:id="@+id/phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:clickable="false"
            android:ellipsize="end"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:maxLines="1"
            android:text="16600000000"
            android:textColor="@color/black1"
            android:textSize="14dp"
            app:layout_constraintTop_toTopOf="@id/contact"
            app:layout_constraintBottom_toBottomOf="@id/contact"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/contact"
            app:layout_constraintHorizontal_chainStyle="spread_inside"/>

在联系人长度比较短的情况下,可以正常显示,但是当联系人内容太长的情况,整行的内容会超出两端,连左右的margin都不生效了,如下图所示

解决方案

只需添加app:layout_constrainedWidth="true"即可,如下代码所示

XML 复制代码
        <TextView
            android:id="@+id/contact"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:clickable="false"
            android:ellipsize="end"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:maxLines="1"
            android:text='收货人:理想很丰满现实很骨感理想很美好现实很残酷'
            android:textColor="@color/black1"
            android:textSize="14dp"
            app:layout_constraintBottom_toTopOf="@id/address"
            app:layout_constraintStart_toEndOf="@id/tv_first"
            app:layout_constraintEnd_toStartOf="@id/phone"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintHorizontal_chainStyle="spread_inside"
            app:layout_constrainedWidth="true"/>
        <TextView
            android:id="@+id/phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:clickable="false"
            android:ellipsize="end"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:maxLines="1"
            android:text="16600000000"
            android:textColor="@color/black1"
            android:textSize="14dp"
            app:layout_constraintTop_toTopOf="@id/contact"
            app:layout_constraintBottom_toBottomOf="@id/contact"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/contact"
            app:layout_constraintHorizontal_chainStyle="spread_inside"
            app:layout_constrainedWidth="true"/>

效果如下

由于设置了android:ellipsize="end",所以超出的部分会显示省略号...

相关推荐
GitLqr14 小时前
Flutter 3.44 插件内置 Kotlin (KGP) 双向兼容适配指南
android·flutter·dart
随遇丿而安18 小时前
第11周:Activity 跳转与传值 + 跳转优化
android
私人珍藏库19 小时前
[Android] BBLL 开源第三方B哩电视TV端
android·app·生活·工具·多功能
杉氧21 小时前
跨平台资源管理:一套代码如何搞定 Android、iOS 和 Web 的图片与多语言?
android·架构·android jetpack
安卓修改大师1 天前
安卓修改大师实战:从反编译到定制的完整APK修改指南
android
柚鸥ASO优化1 天前
安卓APP推广的“降本增效”密码:守好商店内,打好商店外
android·aso优化
我是一颗柠檬1 天前
【Java项目技术亮点】EXPLAIN深度分析与慢查询治理
android·java·开发语言
Android-Flutter1 天前
android compose shadow 阴影 使用
android·kotlin·compose
帅次1 天前
Android 高级工程师面试:Java 多线程与并发 近1年高频追问 22 题
android·java·面试
2501_943782351 天前
【共创季稿事节】摩斯电码转换器:编码表与双向转换的实现
android·华为·鸿蒙·鸿蒙系统