日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【约束布局】ConstraintLayout 组件可见性 View.GONE 处理 与 Margin 属性 ( 约束布局可见性处理 | goneMargin 属性 )

發(fā)布時間:2025/6/17 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【约束布局】ConstraintLayout 组件可见性 View.GONE 处理 与 Margin 属性 ( 约束布局可见性处理 | goneMargin 属性 ) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

        • I . 相對定位 Margin 屬性
        • II . 可見性改變后的行為處理 ( Visibility Behavior )
        • III . 組件設(shè)置 GONE 屬性示例
        • 被 約束到不可見組件 后 Margin 屬性 表現(xiàn)
        • IV . goneMargin 屬性示例
        • V . 相關(guān)資源代碼



I . 相對定位 Margin 屬性


相對定位 Margin 簡介 :

  • 1.Margin 作用對象 : 如果某一邊對應(yīng)的 Margin 屬性被設(shè)置 , 這些 Margin 會被應(yīng)用于對應(yīng)的約束 ;
  • 2.Margin 作用效果 : 設(shè)置 Margin 屬性會強(qiáng)制在 被約束的邊 ( 源邊 Source Side )約束到的位置 之間 填充指定尺寸的空間 ;
  • 3.Margin 屬性類型及取值范圍 : Margin 屬性值是尺寸類型 , 設(shè)置 dip , px 等數(shù)值屬性 , 其值必須大于等于0 ;
  • 4.常用的相對定位 Margin 屬性 :
//左側(cè)邊的空白 android:layout_marginStart //右側(cè)邊的空白 android:layout_marginEnd//左側(cè)邊的空白 android:layout_marginLeft //右側(cè)邊的空白 android:layout_marginRight//頂部邊的空白 android:layout_marginTop //底部邊的空白 android:layout_marginBottom


II . 可見性改變后的行為處理 ( Visibility Behavior )


約束布局 組件 可見性說明 :

  • 1.討論情況 : 約束布局中 , 當(dāng)組件被設(shè)置成 View.GONE 可見性時 , 針對該組件的處理方式 ;
  • 2.通常處理方案 ( 其它布局 ) : 被設(shè)置成 View.GONE 屬性的組件 , 一般情況下進(jìn)行如下處理 :
    • ① 取消顯示 : 首先其先取消顯示 ;
    • ② 布局移除 : 然后將該組件從布局中移除 ;
    • ③ 尺寸不變 : 但組件的實(shí)際的尺寸不會被修改 ;
  • 3.約束布局消失的組件處理方式 :
    • ① 取消顯示 : 該組件消失 , 不可見 ;
    • ② 布局保留 : 該組件仍然是布局中的一部分 ;
    • ③ 尺寸修改 : 該組件的寬高尺寸會被設(shè)置成 0 , 即 該組件收縮成一個點(diǎn) ;
    • ④ 約束保留 : 該組件 四個方向的約束 仍然有效 , 其所在位置不會改變 ;
    • ⑤ Margin 置 0 : 該組件所有方向的 Margin 屬性都被設(shè)置成 0 ; 即 下面 的 6 種 Margin 屬性清零 ;
layout_goneMarginStart layout_goneMarginEnd layout_goneMarginLeft layout_goneMarginTop layout_goneMarginRight layout_goneMarginBottom


III . 組件設(shè)置 GONE 屬性示例


組件設(shè)置 GONE 屬性 改變示例 :

  • 1.布局示例 : 該布局中 , 含有約束組件和被約束組件 , 代碼如下 :
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayoutxmlns: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"tools:context=".MainActivity"tools:layout_editor_absoluteY="25dp"><!--被 約束 的組件該組件 垂直方向 居中于整體布局其左側(cè) 緊貼 目標(biāo)組件 , 右側(cè)約束于 父容器--><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="被約束組件"app:layout_constraintStart_toEndOf="@+id/button"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.0"app:layout_constraintTop_toTopOf="parent"app:layout_constraintBottom_toBottomOf="parent" /><!--約束 到的 目標(biāo)組件該組件 垂直方向 居中于整體布局其左側(cè)緊貼布局左邊緣 , 但是有 50dip 的邊距 Margin如果該組件設(shè)置不可見 , 就會變成一個點(diǎn) , 其 50dip 的邊距也會消失如果設(shè)置了 layout_goneMarginLeft 屬性 , 其消失狀態(tài)下 , 該邊距有效--><Buttonandroid:id="@+id/button"android:layout_width="188dp"android:layout_height="188dp"android:text="目標(biāo)組件"android:visibility="visible"app:layout_constraintStart_toStartOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.0"android:layout_marginLeft="50dip"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintVertical_bias="0.5" /></android.support.constraint.ConstraintLayout>
  • 2.布局截圖 :
  • 3.布局說明 :
    • ① 目標(biāo)組件 : 該組件的 android:visibility 屬性稍后會被設(shè)置 View.GONE;
      • 1> 垂直方向 : 居中于布局 ;
      • 2> 水平方向 : 其約束與父容器左右 , Bias 屬性為 0 , 因此其緊貼左側(cè) , 由于設(shè)置了 layout_marginLeft 屬性 為 50dip , 其組件的左側(cè) 舉例 父容器左邊緣有 50dip 的空隙 ;
    • ② 被約束組件 : 該組件 被約束與目標(biāo)組件 , 主要是查看 目標(biāo)組件消失后 , 其被約束的狀況 ;
      • 1> 垂直方向 : 居中于布局 ;
      • 2> 水平方向 : 其左側(cè) 約束與 目標(biāo)組件右側(cè) , 其右側(cè)約束與父容器右側(cè) , Bias 屬性為 0 , 因此 其緊貼 目標(biāo)組件的右側(cè) ;
  • 4.設(shè)置目標(biāo)組件 GONE 屬性 :
android:visibility="visible"
  • 5.設(shè)置 GONE 屬性后代碼 : 與上面的代碼唯一不同就是 button 組件 ( 目標(biāo)組件 ) 可見性被設(shè)置成了 GONE ;
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayoutxmlns: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"tools:context=".MainActivity"tools:layout_editor_absoluteY="25dp"><!--被 約束 的組件該組件 垂直方向 居中于整體布局其左側(cè) 緊貼 目標(biāo)組件 , 右側(cè)約束于 父容器--><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="被約束組件"app:layout_constraintStart_toEndOf="@+id/button"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.0"app:layout_constraintTop_toTopOf="parent"app:layout_constraintBottom_toBottomOf="parent" /><!--約束 到的 目標(biāo)組件該組件 垂直方向 居中于整體布局其左側(cè)緊貼布局左邊緣 , 但是有 50dip 的邊距 Margin如果該組件設(shè)置不可見 , 就會變成一個點(diǎn) , 其 50dip 的邊距也會消失如果設(shè)置了 layout_goneMarginLeft 屬性 , 其消失狀態(tài)下 , 該邊距有效--><Buttonandroid:id="@+id/button"android:layout_width="188dp"android:layout_height="188dp"android:text="目標(biāo)組件"android:visibility="gone"app:layout_constraintStart_toStartOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.0"android:layout_marginLeft="50dip"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintVertical_bias="0.5" /></android.support.constraint.ConstraintLayout>
  • 6.組件設(shè)置 GONE 屬性布局截圖 :
  • 7.組件設(shè)置 GONE 屬性后的說明 :
    • ① 可見性與寬高 : 目標(biāo)組件仍然在布局中存在 , 只是變成了一個點(diǎn) , 寬高變成 0 , 不可見 ;
    • ② 約束存在 : 目標(biāo)組件的約束仍然有效 , 垂直方向上 , 仍然居中 , 水平方向緊貼左側(cè) , 符合約束設(shè)置 ;
    • ③ Margin 清零 : 左側(cè) Margin 被設(shè)置成了 0dip , 原來的值是 50dip , 組件消失后 , 其值變成 0 ;



被 約束到不可見組件 后 Margin 屬性 表現(xiàn)


當(dāng) 被約束組件的某位置 ( Left, Right, Top, Bottom ) 被 約束到的目標(biāo)組件可見性 ( Visibility ) 是 View.GONE 時 , 可以使用下面的屬性值 , 設(shè)置一個不同的 Margin 屬性值 ;

layout_goneMarginStart layout_goneMarginEnd layout_goneMarginLeft layout_goneMarginTop layout_goneMarginRight layout_goneMarginBottom

注意 : 該屬性是設(shè)置給 可見組件的 , 當(dāng)該組件被約束到的組件不可見時 , 該屬性生效 ;




IV . goneMargin 屬性示例


goneMargin 屬性示例 :

  • 1.目標(biāo)組件可見時 代碼 :
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayoutxmlns: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"tools:context=".MainActivity"tools:layout_editor_absoluteY="25dp"><!--被 約束 的組件該組件 垂直方向 居中于整體布局其左側(cè) 緊貼 目標(biāo)組件 , 右側(cè)約束于 父容器--><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="被約束組件"app:layout_goneMarginLeft="50dip"app:layout_constraintStart_toEndOf="@+id/button"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.0"app:layout_constraintTop_toTopOf="parent"app:layout_constraintBottom_toBottomOf="parent" /><!--約束 到的 目標(biāo)組件該組件 垂直方向 居中于整體布局其左側(cè)緊貼布局左邊緣 , 但是有 50dip 的邊距 Margin如果該組件設(shè)置不可見 , 就會變成一個點(diǎn) , 其 50dip 的邊距也會消失如果設(shè)置了 layout_goneMarginLeft 屬性 , 其消失狀態(tài)下 , 該邊距有效--><Buttonandroid:id="@+id/button"android:layout_width="188dp"android:layout_height="188dp"android:text="目標(biāo)組件"android:visibility="visible"app:layout_constraintStart_toStartOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.0"android:layout_marginLeft="50dip"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintVertical_bias="0.5" /></android.support.constraint.ConstraintLayout>
  • 2.目標(biāo)組件可見時 截圖 :
  • 3.目標(biāo)組件不可見時 且 沒有 goneMargin 屬性時 的 代碼 :
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayoutxmlns: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"tools:context=".MainActivity"tools:layout_editor_absoluteY="25dp"><!--被 約束 的組件該組件 垂直方向 居中于整體布局其左側(cè) 緊貼 目標(biāo)組件 , 右側(cè)約束于 父容器--><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="被約束組件"app:layout_constraintStart_toEndOf="@+id/button"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.0"app:layout_constraintTop_toTopOf="parent"app:layout_constraintBottom_toBottomOf="parent" /><!--約束 到的 目標(biāo)組件該組件 垂直方向 居中于整體布局其左側(cè)緊貼布局左邊緣 , 但是有 50dip 的邊距 Margin如果該組件設(shè)置不可見 , 就會變成一個點(diǎn) , 其 50dip 的邊距也會消失如果設(shè)置了 layout_goneMarginLeft 屬性 , 其消失狀態(tài)下 , 該邊距有效--><Buttonandroid:id="@+id/button"android:layout_width="188dp"android:layout_height="188dp"android:text="目標(biāo)組件"android:visibility="gone"app:layout_constraintStart_toStartOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.0"android:layout_marginLeft="50dip"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintVertical_bias="0.5" /></android.support.constraint.ConstraintLayout>
  • 4.目標(biāo)組件不可見時 且 沒有 goneMargin 屬性時 的 截圖 :
  • 5.目標(biāo)組件不可見時 且 有 goneMargin 屬性時 的 代碼 :
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayoutxmlns: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"tools:context=".MainActivity"tools:layout_editor_absoluteY="25dp"><!--被 約束 的組件該組件 垂直方向 居中于整體布局其左側(cè) 緊貼 目標(biāo)組件 , 右側(cè)約束于 父容器--><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="被約束組件"app:layout_goneMarginLeft="50dip"app:layout_constraintStart_toEndOf="@+id/button"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.0"app:layout_constraintTop_toTopOf="parent"app:layout_constraintBottom_toBottomOf="parent" /><!--約束 到的 目標(biāo)組件該組件 垂直方向 居中于整體布局其左側(cè)緊貼布局左邊緣 , 但是有 50dip 的邊距 Margin如果該組件設(shè)置不可見 , 就會變成一個點(diǎn) , 其 50dip 的邊距也會消失如果設(shè)置了 layout_goneMarginLeft 屬性 , 其消失狀態(tài)下 , 該邊距有效--><Buttonandroid:id="@+id/button"android:layout_width="188dp"android:layout_height="188dp"android:text="目標(biāo)組件"android:visibility="gone"app:layout_constraintStart_toStartOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.0"android:layout_marginLeft="50dip"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintVertical_bias="0.5" /></android.support.constraint.ConstraintLayout>
  • 6.目標(biāo)組件不可見時 且 有 goneMargin 屬性時 的 截圖 :



V . 相關(guān)資源代碼


相關(guān)代碼地址 :

  • 1.GitHub 項(xiàng)目展示地址 : UI_Demos_4_CSDN_Blog
  • 2.本博客相關(guān)代碼地址 :
    • ① 目標(biāo)組件可見時 且 沒有 goneMargin 屬性時 的 代碼 : activity_gone_behavior_visibile.xml
    • ② 目標(biāo)組件不可見時 且 沒有 goneMargin 屬性時 的 代碼 : activity_gone_behavior_gone.xml
    • ③ 目標(biāo)組件可見時 且 有 goneMargin 屬性時 的 代碼 : activity_gone_behavior_gone_margin_visible.xml
    • ④ 目標(biāo)組件不可見時 且 有 goneMargin 屬性時 的 代碼 : activity_gone_behavior_gone_margin_gone.xml

總結(jié)

以上是生活随笔為你收集整理的【约束布局】ConstraintLayout 组件可见性 View.GONE 处理 与 Margin 属性 ( 约束布局可见性处理 | goneMargin 属性 )的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。