关于ConstraintLayout自适应高度遇到的坑

2023-05-16


关于ConstraintLayout自适应高度遇到的坑   记录下来
android:layout_height="wrap_content"
为了缩减嵌套层及采用了ConstraintLayout作为dialog布局,但是发现dialog下边的确定按钮总是被盖一部分
线上问题代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="@drawable/dialog_bg">
    <ImageView
        android:id="@+id/type_uploading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="66px"
        android:src="@mipmap/success_uploading"
        android:visibility="gone"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <TextView
        android:id="@+id/tv_type"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="80px"
        android:layout_marginTop="70px"
        android:layout_marginRight="80px"
        android:gravity="center"
        android:text="上传成功"
        android:textColor="@color/widthdrawnoticestr"
        android:textSize="17sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/type_uploading" />

        <--描述文字默认隐藏-->
    <TextView
        android:id="@+id/tv_type_summary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30px"
        android:text="描述文字"
        android:visibility="gone"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_type" />
    <ImageView
        android:id="@+id/img_division"
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:layout_marginTop="59px"
        android:background="@color/division_line"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_type_summary" />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/img_division">
        <TextView
            android:id="@+id/tv_erro_ok"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="@string/ok"
            android:textColor="@color/widthdrawnoticestr"
            android:textSize="17sp"
            android:visibility="gone" />
          <--其他布局-->
    </RelativeLayout>
</android.support.constraint.ConstraintLayout>

以上代码有一下问题 
(1)上下控件衔接并没有连贯 控件没有完全指定上边或下界约束控件
(2)如果出现visible=gone这个情况需要嵌套一个布局 否则约束链断了,同样出现布局问题
(3)这个布局并不复杂使用RelativeLayout完全可以了还可以省略很多代码

修改代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="@drawable/dialog_bg">
    <LinearLayout
        android:id="@+id/ln_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintBottom_toTopOf="@+id/img_division"
        app:layout_constraintTop_toTopOf="parent">
        <ImageView
            android:id="@+id/type_uploading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="66px"
            android:src="@mipmap/success_uploading"
            android:visibility="gone" />
        <TextView
            android:id="@+id/tv_type"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="80px"
            android:layout_marginTop="70px"
            android:layout_marginRight="80px"
            android:gravity="center"
            android:text="@string/article_uploading"
            android:textColor="@color/widthdrawnoticestr"
            android:textSize="17sp" />
        <TextView
            android:id="@+id/tv_type_summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="30px"
            android:text="今日已达上限"
            android:visibility="gone" />
    </LinearLayout>

    <ImageView
        android:id="@+id/img_division"
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:layout_marginTop="59px"
        android:background="@color/division_line"
        app:layout_constraintBottom_toTopOf="@+id/rl_bootom"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ln_top" />
    <RelativeLayout
        android:id="@+id/rl_bootom"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/img_division">
        <TextView
            android:id="@+id/tv_erro_ok"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:text="@string/ok"
            android:textColor="@color/widthdrawnoticestr"
            android:textSize="17sp"
            android:visibility="visible" />
        <--其他布局-->
    </RelativeLayout>
</android.support.constraint.ConstraintLayout>
总结:RelativeLayout 相对位置 left top right bottom 在设置相对属性的时候回失效
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"其他情况没有问题
ConstraintLayout 控件布局不能有断链,否则约束布局会失效


 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

关于ConstraintLayout自适应高度遇到的坑 的相关文章

  • gdebi来安装依赖关系

    gdebi是一个用于安装你自己手动下载的包的GUI程序 GDebi也可以命令行模式运行 xff08 sudo gdebipackage deb xff09 xff0c 其功能和GUI模式下完全一样 安装 xff1a apt get inst
  • &和&&的区别?

    答 xff1a amp 运算符有两种用法 xff1a 1 按位与 xff1b 2 逻辑与 amp amp 运算符是短路与运算 逻辑与跟短路与的差别是非常巨大的 xff0c 虽然二者都要求运算符左右两端的布尔值都是true整个表达式的值才是t
  • Spring的五种依赖注入方式

    平常的java开发中 xff0c 程序员在某个类中需要依赖其它类的方法 xff0c 则通常是new一个依赖类再调用类实例的方法 xff0c 这种开发存在的问题是new的类实例不好统一管理 xff0c spring提出了依赖注入的思想 xff
  • Unity VR游戏开发干货教程:优化VR体验

    简介 对于VR应用来说 xff0c 如果想要让用户获得好的用户体验 xff0c 特别是免除恶心眩晕的困扰 xff0c 在VR开发中进行优化是必不可少的 xff0c 惟其如此才能达到我们期望的游戏运行帧速 和其它平台上的开发不同 xff0c
  • 使用lombok编译时报错:程序包org.slf4j不存在

    原文链接 xff1a http www jylt cc detail id 61 67987702f9160c26a14d3a421f43dce1 在使用lombok插件打印日志时 xff0c 编译时候报错 xff0c 只需做如下修改即可
  • 企业对C/C++程序员的技能要求

    一个人应该具备对事物的思考能力 xff0c 否则容易被忽悠 对大部分未入门或刚入门的菜鸟来说 xff0c 很难搞明白C语言能做什么和C程序员在做什么这两个问题 如果你打算种菜 xff0c 必须先了解行情 xff08 包括销量和价钱 xff0
  • 如何让 Shell 提示符更酷炫

    使用远程终端时 xff0c 默认的命令行提示符格式已经能满足大部分用户需求了 xff0c 但有时我们希望提示符看起来更直观 优雅 酷炫 美观 xff0c 可以从中直接得到我们想要的信息 xff0c 而且清晰分明 本文就详细讲解一下如何让 S
  • 写给大侄女

    老姑从你上高中开始 xff0c 就想写点东西给大侄女看 xff0c 不过老姑理科出身 xff0c 文笔比较差 不知道该不该提你在学校看手机的事情 xff0c 老姑没有责备你的意思 xff0c 只是和你探讨一下 xff0c 毕竟谁没有年轻的时
  • centos安装lspci工具

    背景 由于centos6 3迷你安装版上没有带lspci工具 在定制内核时 无法用此工具查询硬件相关信息 具体步骤如下 1 下载 pci包 xff1a http www kernel org pub software utils pciut
  • 软件性能测试方法论

    软件性能测试过程详解与案例分析 xff08 段念 编著 xff09 学习笔记三 1 SEI负载测试计划过程 SEI load Testing Planning Process是一个关注于负载测试计划的方法 xff0c 其目标是产生 清晰 易
  • Android Studio使用Kotlin时Execution failed for task ':app.compileDebugKotlin'.问题

    最近在接触kotlin编写android xff0c 有些坑必须得踩 kotlin插件依赖添加成功以后 xff0c 突然爆一个Execution failed for task 39 app compileDebugKotlin 39 go
  • HTTP Get,Post请求详解

    请求类型 三种最常见的请求类型是 xff1a GET xff0c POST 和 HEAD GET xff1a 获取一个文档 大部分被传输到浏览器的html xff0c images xff0c js xff0c css 都是通过GET方法发
  • Linux查看端口使用状态、关闭端口方法

    前提 xff1a 首先你必须知道 xff0c 端口不是独立存在的 xff0c 它是依附于进程的 某个进程开启 xff0c 那么它对应的端口就开启了 xff0c 进程关闭 xff0c 则该端口也就关闭了 下次若某个进程再次开启 xff0c 则
  • 查找列表中某个值的位置(python)

    p 61 list index value list为列表的名字 value为查找的值 p为value在list的位置 以下内容引自 xff1a http www linuxidc com Linux 2012 01 51638 htm P

随机推荐