一、子元素不被挤出父容器边界
1、两个子元素,前者可以随内容宽度缩放,后者可以跟随前者移动
2、后者不被挤出边界(借助Guideline)
java
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/edit_title_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="@dimen/dp10"
android:layout_toRightOf="@+id/change_avatar">
<EditText
android:id="@+id/edit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:hint="好标题会吸引人气"
app:layout_constraintHorizontal_bias="0"
android:maxLength="20"
android:singleLine="true"
android:textColor="@color/white"
android:textColorHint="#B3ffffff"
android:textSize="15dp"
app:layout_constraintEnd_toStartOf="@+id/image_edit"
app:layout_constraintHorizontal_weight="1"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
tools:text="我是正在直播好的"
tools:ignore="MissingConstraints" />
<ImageView
android:id="@+id/image_edit"
android:layout_width="13dp"
android:layout_height="13dp"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_gravity="center_vertical"
app:layout_constraintHorizontal_chainStyle="packed"
android:layout_marginLeft="@dimen/dp4"
android:alpha="0.7"
android:src="@drawable/icon_create_live_edit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/guideline"
app:layout_constraintStart_toEndOf="@+id/edit_name"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="1" />
</androidx.constraintlayout.widget.ConstraintLayout>
二、多个子元素,其中一个TextView宽度自适应且超过父容器的剩下宽度后内部自动...
1、view1固定最左侧,view2宽度自适应,view3跟随view2, view4一直在最右9侧。
2、view2超过父容器的剩下宽度后,内部自动..., 且不挤出view3
java
<?xml version="1.0" encoding="utf-8"?>
<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="@dimen/dp56"
tools:background="@color/black"
tools:ignore="MissingDefaultResource,ResourceName"
>
<CircleAvatarView
android:id="@+id/view1"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_centerVertical="true"
android:src="@drawable/ic_default_avatar"
app:autoMask="false"
app:autoPlaceholder="false"
app:businessType="community"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/sender_name"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:placeholderImage="@drawable/ic_default_avatar"
app:roundAsCircle="true"
/>
<TextView
android:id="@+id/view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="@dimen/dp8"
android:singleLine="true"
android:textColor="@color/white"
android:textSize="14dp"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/gift_num_view"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toEndOf="@+id/avatar"
app:layout_constraintTop_toTopOf="parent"
tools:text="昵称昵称"
/>
<GiftNumView
android:id="@+id/view3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="@dimen/dp20"
android:layout_marginEnd="16dp"
android:layout_toLeftOf="@+id/diamond_num"
android:layout_toRightOf="@+id/sender_name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/diamond_num"
app:layout_constraintStart_toEndOf="@+id/sender_name"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="@+id/view4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:alpha="0.5"
android:textColor="@color/GBK99B"
android:textSize="14dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="1200 盐钻"
/>
</com.zhihu.android.base.widget.ZHConstraintLayout>
三、PopWindow中textView高度自适应,但是撑不起来
1、textView中内容高度自使用可滑动(NestedScrollView包textView)
2、设置最大高度(ConstraintLayout中NestedScrollView设置maxHeight)
java
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp12"
>
<androidx.core.widget.NestedScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fadingEdge="vertical"
android:fadingEdgeLength="@dimen/dp20"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHeight_max="120dp"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints"
>
<TextView
android:id="@+id/notice_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="1"
android:ellipsize="marquee"
android:fadingEdge="vertical"
android:fadingEdgeLength="20dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:lineSpacingMultiplier="1.2"
android:marqueeRepeatLimit="marquee_forever"
android:textColor="@color/white"
android:textSize="15dp"
tools:text="我的名字我的名字我的名字我的名字我的名字我的名字我的名字"
/>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>