文章目录

  • ConstraintLayout中子布局wrap_content超出屏幕处理方案
        • 1、问题描述
        • 2、布局效果展示
        • 3、问题代码
        • 4、解决方案
        • 5、附录


ConstraintLayout中子布局wrap_content超出屏幕处理方案

1、问题描述

在ConstraintLayout中使用链式约束布局,且子控件宽度设置为wrap_content,无其他强制宽度约束,布局效果默认各子控件最大宽度/高度=ConstraintLayout宽/高,导致子控件可能超出ConstraintLayout布局范围
注:引入版本–‘androidx.constraintlayout:constraintlayout:1.1.3’

2、布局效果展示


3、问题代码

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android/apk/res/android"
    xmlns:app="http://schemas.android/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white">

    <ImageView
        android:id="@+id/img_icon"
        ...
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

       <TextView
           android:id="@+id/tv_title"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           ...
           app:layout_constraintBottom_toTopOf="@id/tv_subTitle"
           app:layout_constraintRight_toRightOf="@id/msg_red_dot"
           app:layout_constraintLeft_toRightOf="@id/img_icon"
           app:layout_constraintTop_toTopOf="parent" />

       <com.flyco.tablayout.widget.MsgView
           android:id="@+id/msg_red_dot"
           android:layout_width="5dp"
           android:layout_height="5dp"
           ...
           app:layout_constraintLeft_toRightOf="@id/tv_title"
           app:layout_constraintRight_toRightOf="parent"
           app:layout_constraintTop_toTopOf="@id/tv_title" />

    <TextView
        android:id="@+id/tv_subTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ...
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@id/img_icon"
        app:layout_constraintTop_toBottomOf="@id/tv_title" />

</androidx.constraintlayout.widget.ConstraintLayout>

4、解决方案

2、添加如下约束宽度

app:layout_constrainedWidth="true"//约束宽度

3、同理高度约束:

  app:layout_constrainedHeight="true"

4、修改后代码

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android/apk/res/android"
    xmlns:app="http://schemas.android/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white">

    <ImageView
        android:id="@+id/img_icon"
    	 ...
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

       <TextView
           android:id="@+id/tv_title"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           ...
           app:layout_constraintHorizontal_chainStyle="packed"  <!-- 见附录1 -->
           app:layout_constraintBottom_toTopOf="@id/tv_subTitle"
           app:layout_constraintRight_toLeftOf="@id/msg_red_dot"
           app:layout_constraintHorizontal_bias="0" <!-- 见附录2 -->
           app:layout_constrainedWidth="true"
           app:layout_constraintLeft_toRightOf="@id/img_icon"
           app:layout_constraintTop_toTopOf="parent" />

       <com.flyco.tablayout.widget.MsgView
           android:id="@+id/msg_red_dot"
           ...
           app:layout_constraintLeft_toRightOf="@id/tv_title"
           app:layout_constraintRight_toRightOf="parent"
           app:layout_constraintTop_toTopOf="@id/tv_title"/>

    <TextView
        android:id="@+id/tv_subTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ...
        app:layout_constrainedWidth="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@id/img_icon"
        app:layout_constraintTop_toBottomOf="@id/tv_title" />
        
</androidx.constraintlayout.widget.ConstraintLayout>

5、附录

1、链式样式设置参数(layout_constraintHorizontal_chainStyle/layout_constraintVertical_chainStyle)

作用于链头第一个子控件
参数说明:

  • spread - 默认样式,散布、默认等分间距(可加权修改间距,可加偏移修改链头/尾间距比例)
  • spread_inside - 类似spread ,但链的两端将不会散布(贴合父布局边界)
  • packed - 整合,链中子元素间不会散布(无间距,非margin和padding),与spread_inside 效果相反(可加偏移修改链头/尾间距比例)

2、偏移设置(layout_constraintHorizontal_bias/layout_constraintVertical_bias)

作用于链头第一个子控件,通过设置值0~1控制头尾间距比例(偏移比例)

更多推荐

ConstraintLayout中子布局wrap_content超出屏幕处理方案