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 这两个属性很简单,使用也方便

猜你喜欢

转载自www.cnblogs.com/zhangqie/p/9720053.html