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 這兩個屬性很簡單,使用也方便


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM