• android -------- ConstraintLayout Group和goneMargin(五)


    前面的文章

    ConstraintLayout 介绍 (一)

    ConstraintLayout约束属性 (二)

    ConstraintLayout 宽高比和偏移量比(三)

    ConstraintLayout Guideline和Barrier(四)

    此博文主要讲解: Group和goneMargin

     

    1:Group

    在开发中,有时候需要同时隐藏或者显示多个控件,用Group就可以很好的实现,是一个辅助类,不会绘制到屏幕上,也不会展现给用户。

    通过属性app:constraint_referenced_ids 将一些 View 组成组进行集体操作,最常见的操作是setVisibility

    <?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"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    
       <!--android.support.constraint.Group(群组)
       -->
    
    
        <TextView
            android:id="@+id/tv1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="@string/app_name"
            android:background="@color/colorAccent"
            app:layout_constraintStart_toStartOf="parent"
            android:gravity="center"
            android:layout_marginTop="15dp"
            app:layout_constraintTop_toTopOf="parent"
            />
    
        <TextView
            android:id="@+id/tv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World"
            android:layout_marginTop="15dp"
            app:layout_constraintStart_toEndOf="@+id/tv1"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginLeft="15dp"
            />
    
        <android.support.constraint.Group
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:constraint_referenced_ids="tv1,tv2"
            android:visibility="visible"
            />
    
    
    
    </android.support.constraint.ConstraintLayout>

    将 android:visibility="gone"  则两个布局都隐藏掉了

    2:goneMargin

    当前View与另一个View绑定后,另一个View的属性设置为了Gone,则以下属性会生效
      layout_goneMarginStart
      layout_goneMarginEnd
      layout_goneMarginLeft
      layout_goneMarginTop
      layout_goneMarginRight
      layout_goneMarginBottom

    代码:

    <?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="match_parent">
    
    
        <!--    app:layout_goneMarginLeft="25dp"
                tv1 设置了android:visibility="gone" 才生效(当前View与另一个View绑定后,
                另一个View的属性设置为了Gone,才会生效)
        -->
    
        <TextView
            android:id="@+id/tv1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="#f5ec7e"
            android:gravity="center"
            android:text="Hello World!"
            app:layout_constraintLeft_toLeftOf="parent"
            android:visibility="gone"
            />
    
        <TextView
            android:id="@+id/tv2"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="#fa6e61"
            android:gravity="center"
            android:text="Hello World!"
            app:layout_constraintLeft_toRightOf="@+id/tv1"
            android:layout_marginLeft="15dp"
            app:layout_goneMarginLeft="55dp"
            />
    
    
    </android.support.constraint.ConstraintLayout>

    Group和goneMargin 这两个属性很简单,使用也方便

  • 相关阅读:
    使用正则匹配数字
    钻石和玻璃球游戏(钻石位置不固定)
    简单绘图
    未解决问题02
    Sqlite3 实现学生信息增删改查
    【Python】科赫雪花绘制
    【Python爬虫】抖音去水印
    【MATLAB】数学计算软件 MathWorks MATLAB R2020a 中文破解版
    【C语言】用指针作为形参完成数据的升序排列
    【C语言】数组名作函数参数完成数据的升序排列
  • 原文地址:https://www.cnblogs.com/zhangqie/p/9720053.html
Copyright © 2020-2023  润新知