ConstraintLayout跟随吸附效果

一、需要的效果
如下图,当前中间的 TextView 内容太长时,不允许把右边的 性别图标 挤的不见了。
在这里插入图片描述
在这里插入图片描述

二、解决方法

几个关键点,
1、外层的ConstraintLayout宽度需要是wrap_content。
2、伸缩子view 和 有可能被挤掉的子view 必须 通过 layout_constraintStart_toStartOf 前后首尾相连,且宽度都是wrap_content。
3、伸缩子view需要添加 app:layout_constrainedWidth=“true” 属性

<?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="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical">

    <com.xxx.yyy.widget.CharmView
        android:id="@+id/charm_view"
        android:layout_width="20dp"
        android:layout_height="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/noble_view"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.xxx.yyy.widget.NobleView
        android:id="@+id/noble_view"
        android:layout_width="20dp"
        android:layout_height="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/shimmer_view"
        app:layout_constraintStart_toEndOf="@+id/charm_view"
        app:layout_constraintTop_toTopOf="parent" />

    <com.xxx.yyy.widget.ShimmerTextView
        android:id="@+id/shimmer_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="2dp"
        android:ellipsize="end"
        android:includeFontPadding="false"
        android:maxLines="1"
        android:textSize="@dimen/font_size_16dp"
        app:layout_constrainedWidth="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/gender_view"
        app:layout_constraintStart_toEndOf="@+id/noble_view"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="测试测试测" />

    <com.xxx.yyy.widget.GenderView
        android:id="@+id/gender_view"
        android:layout_width="18dp"
        android:layout_height="18dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/shimmer_view"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值