一、需要的效果
如下图,当前中间的 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>