分享一种 ConstraintLayout 让TextView 自适应的同时,还不超出限制范围的方式

不知道大家有没有遇到这种布局需求:

上图布局很简单,ImageView + 中间的TextView + View + ImageView,需求是中间的 TextView 宽度需要根据内容来展示,但长度超出屏幕时,需要将 TextView 进行…的折叠展示,同时它后面的 View 和 ImageView 不能被推出屏幕。

当TextView 超长时:

这种需求看起来简单,但暗藏玄机。需要注意的有两点:

  1. TextView 后面的两个 View,要跟随 TextView 的宽度。
  2. TextView 和它后面的两个 View 宽度加起来超出屏幕时,对 TextView 进行折叠,它后面的 View 和 ImageView 不受影响。

这种需求下,常规方案是达不到效果的。
你可能会想到父布局用 LinearLayout,并将 TextView 的宽度设置成 weight ,但这种方案不能满足上面的第一条,因为宽度是一直撑满的。效果是这样:

使用父布局使用 ConstraintLayout ,并用它的一些常规约束,可能会不满足上面的第二条,造成下面的效果(跟随的View 被顶出屏幕):

或者在 Java 代码中,拿到 TextView 可用宽度,并设置成它的 maxWidth,这种方案虽然可行,但实现起来还是比较复杂。我还是希望在 xml 布局中就能表达出这种约束关系。

解决方案:

方案一:

主要属性:

  • app:layout_constrainedWidth=“true”:是否强制约束
  • app:layout_constraintHorizontal_bias=“0.0”:布局相对横向约束空间的位置百分比

源代码:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:padding="16dp"><ImageViewandroid:id="@+id/product_seller_avatar"android:layout_width="16dp"android:layout_height="16dp"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"tools:background="@color/common_red" /><TextViewandroid:id="@+id/product_seller_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="3dp"android:ellipsize="end"android:maxLines="1"android:textColor="#999"android:textSize="11dp"app:layout_constrainedWidth="true"app:layout_constraintEnd_toStartOf="@+id/point"app:layout_constraintStart_toEndOf="@+id/product_seller_avatar"app:layout_constraintTop_toTopOf="parent"tools:text="诗酒趁年华诗酒趁年华诗酒趁年华诗酒趁年华诗酒趁年华诗酒趁年华诗酒趁年华" /><Viewandroid:id="@+id/point"android:layout_width="2dp"android:layout_height="2dp"android:layout_marginStart="6dp"android:background="@drawable/vector_level_manager"app:layout_constraintStart_toEndOf="@+id/product_seller_name"app:layout_constraintTop_toTopOf="parent" /><ImageViewandroid:id="@+id/product_group_shop_level_icon"android:layout_width="12dp"android:layout_height="12dp"android:layout_marginStart="6dp"android:src="@drawable/vector_level_manager"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.0"app:layout_constraintStart_toEndOf="@+id/point"app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>

需要注意的是,TextView 后面跟随的 View 数量一定要大于1,否则 TextView 和 View 会形成链,从而导致下面这种情况:

方案二:

主要属性:

  • app:layout_constraintWidth_default=“wrap”:仅在需要时扩展视图以适应其内容,但如有约束条件限制,视图仍然可以小于其内容。因此,它与使用 Wrap Content 之间的区别在于,将宽度设为 Wrap Content 会强行使宽度始终与内容宽度完全匹配;而使用 layout_constraintWidth_default 设置为 wrapMatch Constraints 时,视图可以小于内容宽度。
  • app:layout_constraintHorizontal_chainStyle=“packed”:横向链风格,将链中元素居中在链条的中心
  • app:layout_constraintHorizontal_bias=“0.0”

源代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:padding="16dp"><ImageViewandroid:id="@+id/product_seller_avatar"android:layout_width="16dp"android:layout_height="16dp"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"tools:background="@color/common_red" /><TextViewandroid:id="@+id/product_seller_name"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginStart="3dp"android:ellipsize="end"android:maxLines="1"android:textColor="#999"android:textSize="11dp"app:layout_constraintEnd_toStartOf="@+id/point"app:layout_constraintHorizontal_bias="0.0"app:layout_constraintHorizontal_chainStyle="packed"app:layout_constraintStart_toEndOf="@+id/product_seller_avatar"app:layout_constraintTop_toTopOf="parent"app:layout_constraintWidth_default="wrap"tools:text="诗酒趁年华诗酒趁年华诗酒趁诗酒趁年华诗酒趁年华诗酒趁诗酒趁年华诗酒趁年华诗酒趁诗酒趁年华诗酒趁年华诗酒趁" /><Viewandroid:id="@+id/point"android:layout_width="2dp"android:layout_height="2dp"android:layout_marginStart="6dp"android:background="@drawable/vector_level_manager"app:layout_constraintEnd_toStartOf="@+id/product_group_shop_level_icon"app:layout_constraintStart_toEndOf="@+id/product_seller_name"app:layout_constraintTop_toTopOf="parent" /><ImageViewandroid:id="@+id/product_group_shop_level_icon"android:layout_width="12dp"android:layout_height="12dp"android:layout_marginStart="6dp"android:src="@drawable/vector_level_manager"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toEndOf="@+id/point"app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>

与方案一相反,这种方案一定需要 TextView 和它后面的 View 形成横向的链,才能生效。

最后

ConstraintLayout 非常强大,它更多的属性可以参考:https://developer.android.com/training/constraint-layout?hl=zh-cn

分享一种 ConstraintLayout 让TextView 自适应的同时,还不超出限制范围的方式相关推荐

  1. 新分享一种解决ie6下PNG图片透明的方法

    IE6中的图片不透明问题一直困扰着广大设计从业者们,前段时间已经分享了一种使用HTC文件解决的方法和使用滤镜使png背景图片透明的方法,今天再分享一种使用脚本代码使PNG图片在IE6中透明的办法. 脚 ...

  2. 收藏 | 分享 3 种脑洞大开的Excel技巧

    全世界只有3.14 % 的人关注了 数据与算法之美 身为职场人,Excel基本是每天都会打开的软件,如果把对它的使用熟练程度分个等级,大概可以分为几下几种: Level 1: 对Excel的基本功能已 ...

  3. 电脑连接电视方法详解_笔记本连接电视方法有哪些?分享两种笔记本连接电视方法...

    在如今这个快节奏的时代中,大多数年轻人的手中都会有个笔记本电脑,并且将笔记本电脑作为休闲娱乐或办公的一种设备.相比于电视屏幕尺寸,笔记本电脑的屏幕还真是小了很多.为了提高视觉上的体验,很多小伙伴会选择 ...

  4. 分享一种中小企业的文件服务器方案

    分享一种中小企业的文件服务器方案 以我所在的单位为例,目前我们的文件共享都是使用samba+网络盘的方式,这种方式的好处就是便利,但也有很多不爽的地方,samba管理用户相当啰嗦,比方添加一个新用户, ...

  5. android textview 动态高度自适应,TextView自适应高度(解决_UITextContainerView布局问题)...

    对于自定义创建回复框, textView自适应高度, 根据textView.contentSize.height调节高度时, 有时候会有textView换行时文字跳动过高, 不是平滑过渡. 请参考一下 ...

  6. 分享一种固定页教在页面底部的方法

    这里是固定在页面底部,而不是fixed浏览屏幕的底部.如下 这样的方法有很多,这里分享一种本人用过的.在last内容的div加上padding-bottom. <!DOCTYPE html> ...

  7. 通过stream去重_分享几种 Java8 中通过 Stream 对列表进行去重的方法

    几种列表去重的方法 在这里我来分享几种列表去重的方法,算是一次整理吧,如有纰漏,请不吝赐教.1. Stream 的distinct()方法 distinct()是Java8中 Stream 提供的方法 ...

  8. 如何低成本快速积累用户,分享4种无脑式操作

    马云曾说过这样一句话,把梳子卖给和尚,这根本不算什么营销技巧,而是骗术,原因很简单,因为和尚根本就不需要梳子,你卖给人家不就是骗人家吗? 其实,马云说这句话的重点是:找对目标用户,不是人人都是你的客户 ...

  9. 如何制作趣味头像?分享几种制作头像的方法

    怎么制作出趣味头像呢?制作出趣味头像可以用来玩社交媒体上的各种有趣的游戏,或者在聊天应用程序中使用,让你的聊天更加生动有趣.此外,你也可以将头像用作个人品牌或网站的标识,增加品牌的辨识度.另外,头像也 ...

最新文章

  1. GitHub 的项目徽章
  2. 简单自学机器学习理论——正则化和偏置方差的权衡 (Part III )
  3. hihoCoder #1468 : 2-SAT·hihoCoder新春晚会(2-SAT 输出字典序最小的方案)
  4. WEB消息推送-comet4j
  5. Python3——通用序列操作
  6. 2022牛客寒假算法基础集训营(一)1全部题解
  7. 中间件学习——J2EE中间件四步曲
  8. 计算机视觉教程2-3:图解双线性插值算法最近邻插值算法
  9. 小米2S稳定版 教你如何一键ROOT
  10. 软件测试方案和计划的区别和联系
  11. Excel如何批量设置行高
  12. Circular Coloring
  13. win10 蓝牙忽然消失,设备管理器有未知USB设备描述符请求失败
  14. Vimdiff 使用
  15. 苹果微信多开_苹果手机微信双开,微信多开有哪些可以推荐的?
  16. tftp 在嵌入式设备和主机之间传输文件
  17. 如何知道服务器支持301,301页面跳转的方法---从服务器上设置方法
  18. 史上最“牛”,荣辱参半的的JAVA群规和QQ群
  19. 苹果切换输入法_轻松搞定缅甸语手机输入法(苹果篇)
  20. 卡巴斯基互联网安全套装(KIS)7.0官方简体中文版

热门文章

  1. 笔记本未启用无线服务器,WiFi无线网络提示未启用DHCP无法上网的解决方法
  2. cs224w(图机器学习)2021冬季课程学习笔记4 Link Analysis: PageRank (Graph as Matrix)
  3. macOS下OneNote中文字体和数字英文字体不统一,英文自动变粗体
  4. erp系统都有哪些品牌
  5. 通达信程序化交易接口的设计方案
  6. node-red教程3.3 file控件介绍
  7. OpenVINO示例介绍
  8. 【复试英语】应对考官三招解决尴尬局面!
  9. unity中awak,start,update之间的关系
  10. java 日历工具_【Java】太强了,这款开源日历工具库堪称神器!