xml布局边界记录

一、子元素不被挤出父容器边界

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>
相关推荐
CS Beginner5 天前
【JavaWeb学习】myabtis.xml一次性加载mapper相关的文件
xml·学习
C嘎嘎嵌入式开发5 天前
(21)100天python从入门到拿捏《XML 数据解析》
xml·开发语言·python
BTU_YC5 天前
DrawIO PPT模板自动生成指南:从文字排版到XML输出的完整工作流
xml·draw.io
源力祁老师5 天前
ODOO数据文件(XML、CSV、SQL)是如何转换并加载到 Odoo 数据库
xml·数据库·sql
爱吃土豆的马铃薯ㅤㅤㅤㅤㅤㅤㅤㅤㅤ7 天前
mapper.xml sql动态表查询配置
xml·java·sql
l1t7 天前
DeepSeek辅助利用搬移底层xml实现快速编辑xlsx文件的python程序
xml·开发语言·python·xlsx
ss2738 天前
手写Spring第4弹: Spring框架进化论:15年技术变迁:从XML配置到响应式编程的演进之路
xml·java·开发语言·后端·spring
Lucky_Turtle10 天前
【Java Xml】dom4j写入XML
xml·java·python
莫陌尛.10 天前
xml方式bean的配置---实例化bean的方式
xml
六元七角八分11 天前
pom.xml
xml·数据库