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

编程知识 更新时间:2023-04-05 05:38:33

文章目录

  • 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超出屏幕处理方案

本文发布于:2023-04-05 05:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/890bbed1946672199c92882defc0e863.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中子   布局   屏幕   方案   ConstraintLayout

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 45054文章数
  • 14阅读数
  • 0评论数