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

本文主要是介绍ConstraintLayout中子布局wrap_content超出屏幕处理方案,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 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.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@color/white"><ImageViewandroid:id="@+id/img_icon"...app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid: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.MsgViewandroid: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" /><TextViewandroid: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.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@color/white"><ImageViewandroid:id="@+id/img_icon"...app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid: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.MsgViewandroid:id="@+id/msg_red_dot"...app:layout_constraintLeft_toRightOf="@id/tv_title"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="@id/tv_title"/><TextViewandroid: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超出屏幕处理方案的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/906518

相关文章

MySQL分表自动化创建的实现方案

《MySQL分表自动化创建的实现方案》在数据库应用场景中,随着数据量的不断增长,单表存储数据可能会面临性能瓶颈,例如查询、插入、更新等操作的效率会逐渐降低,分表是一种有效的优化策略,它将数据分散存储在... 目录一、项目目的二、实现过程(一)mysql 事件调度器结合存储过程方式1. 开启事件调度器2. 创

一文详解Python中数据清洗与处理的常用方法

《一文详解Python中数据清洗与处理的常用方法》在数据处理与分析过程中,缺失值、重复值、异常值等问题是常见的挑战,本文总结了多种数据清洗与处理方法,文中的示例代码简洁易懂,有需要的小伙伴可以参考下... 目录缺失值处理重复值处理异常值处理数据类型转换文本清洗数据分组统计数据分箱数据标准化在数据处理与分析过

mysql外键创建不成功/失效如何处理

《mysql外键创建不成功/失效如何处理》文章介绍了在MySQL5.5.40版本中,创建带有外键约束的`stu`和`grade`表时遇到的问题,发现`grade`表的`id`字段没有随着`studen... 当前mysql版本:SELECT VERSION();结果为:5.5.40。在复习mysql外键约

Go语言使用Buffer实现高性能处理字节和字符

《Go语言使用Buffer实现高性能处理字节和字符》在Go中,bytes.Buffer是一个非常高效的类型,用于处理字节数据的读写操作,本文将详细介绍一下如何使用Buffer实现高性能处理字节和... 目录1. bytes.Buffer 的基本用法1.1. 创建和初始化 Buffer1.2. 使用 Writ

Python视频处理库VidGear使用小结

《Python视频处理库VidGear使用小结》VidGear是一个高性能的Python视频处理库,本文主要介绍了Python视频处理库VidGear使用小结,文中通过示例代码介绍的非常详细,对大家的... 目录一、VidGear的安装二、VidGear的主要功能三、VidGear的使用示例四、VidGea

Python结合requests和Cheerio处理网页内容的操作步骤

《Python结合requests和Cheerio处理网页内容的操作步骤》Python因其简洁明了的语法和强大的库支持,成为了编写爬虫程序的首选语言之一,requests库是Python中用于发送HT... 目录一、前言二、环境搭建三、requests库的基本使用四、Cheerio库的基本使用五、结合req

使用Python处理CSV和Excel文件的操作方法

《使用Python处理CSV和Excel文件的操作方法》在数据分析、自动化和日常开发中,CSV和Excel文件是非常常见的数据存储格式,ython提供了强大的工具来读取、编辑和保存这两种文件,满足从基... 目录1. CSV 文件概述和处理方法1.1 CSV 文件格式的基本介绍1.2 使用 python 内

如何使用celery进行异步处理和定时任务(django)

《如何使用celery进行异步处理和定时任务(django)》文章介绍了Celery的基本概念、安装方法、如何使用Celery进行异步任务处理以及如何设置定时任务,通过Celery,可以在Web应用中... 目录一、celery的作用二、安装celery三、使用celery 异步执行任务四、使用celery

SpringBoot操作spark处理hdfs文件的操作方法

《SpringBoot操作spark处理hdfs文件的操作方法》本文介绍了如何使用SpringBoot操作Spark处理HDFS文件,包括导入依赖、配置Spark信息、编写Controller和Ser... 目录SpringBoot操作spark处理hdfs文件1、导入依赖2、配置spark信息3、cont

Java解析JSON的六种方案

《Java解析JSON的六种方案》这篇文章介绍了6种JSON解析方案,包括Jackson、Gson、FastJSON、JsonPath、、手动解析,分别阐述了它们的功能特点、代码示例、高级功能、优缺点... 目录前言1. 使用 Jackson:业界标配功能特点代码示例高级功能优缺点2. 使用 Gson:轻量