前面的文章html
ConstraintLayout 介紹(一)android
ConstraintLayout 寬高比和偏移量比(三)ide
ConstraintLayout Guideline和Barrier(四)佈局
此博文主要講解: Group和goneMarginui
1:Group.net
在開發中,有時候須要同時隱藏或者顯示多個控件,用Group就能夠很好的實現,是一個輔助類,不會繪製到屏幕上,也不會展示給用戶。code
經過屬性app:constraint_referenced_ids
將一些 View 組成組進行集體操做,最多見的操做是setVisibility。xml
<?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" 則兩個佈局都隱藏掉了htm
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 這兩個屬性很簡單,使用也方便