ConstraintLayout佈局介紹.md

一.介紹

ConstraintLayout是一個ViewGroup允許您以靈活的方式定位和調整窗口小部件的窗口。從api9開始支持.繼承自viewGroup;

二.具體使用

這個控件的具體分類主要有如下幾類

1.相對定位

翻譯一下 中間表示相對於自身的某個部位,後面的表示對你設置的那個控件的位置,下圖是位置圖

layout_constraintLeft_toLeftOf =" praent" //表示將自己部位的左邊位於父容器的左邊
layout_constraintLeft_toRightOf
layout_constraintRight_toLeftOf
layout_constraintRight_toRightOf
layout_constraintTop_toTopOf
layout_constraintTop_toBottomOf
layout_constraintBottom_toTopOf
layout_constraintBottom_toBottomOf
layout_constraintBaseline_toBaselineOf
layout_constraintStart_toEndOf
layout_constraintStart_toStartOf
layout_constraintEnd_toStartOf
layout_constraintEnd_toEndOf

2.邊距

  • 這裏和我們的Relative的margin差不多意思

表示如下

android:layout_marginStart
android:layout_marginEnd
android:layout_marginLeft
android:layout_marginTop
android:layout_marginRight
android:layout_marginBottom
  • 當2個view有相對位置的依賴關係,當其中一個view設置1位gone時,這個比較有意思,比方說假設A設置爲gone,後,B需要距離父佈局的左側200dp,怎麼辦?這時候,goneMargin屬性就派上用場啦,只要設置B的layout_goneMarginLeft=200dp即可。這樣,A不爲gone的時候,B距離A 爲android:layout_marginLeft ,A爲gone時,B距離父佈局左側layout_goneMarginLeft200dp。
layout_goneMarginStart
layout_goneMarginEnd
layout_goneMarginLeft
layout_goneMarginTop
layout_goneMarginRight
layout_goneMarginBottom

3.居中定位和偏向定位

  • 1.如果我們沒有 app:layout_constraintVertical_bias="0.7"這行代碼,這個button就處於父容器內的豎直方向的中間位置,這時候如果你要設置比例比如說偏高一點,這是就沒辦法分配了,所以推出了這個控件
<Button
    android:id="@+id/button5"
    android:layout_width="120dp"
    android:layout_height="48dp"
    android:text="第五個"
    app:layout_constraintVertical_bias="0.7"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
   />

定位的方法有豎直和水平偏向2種如下,數值必須處於0-1之間

layout_constraintHorizontal_bias
layout_constraintVertical_bias
  • 2.recycle定位(圓形定位)
layout_constraintCircle :引用另一個小部件ID
layout_constraintCircleRadius :到其他小部件中心的距離
layout_constraintCircleAngle :小部件應該在哪個角度(度數,從0到360)

4.可見性行爲

ConstraintLayout具有標記爲的小部件的特定處理View.GONE。GONE像往常一樣,小部件將不會被顯示,也不是佈局本身的一部分(即,如果標記爲其實際尺寸不會被改變GONE)。但就佈局計算而言,GONE小部件仍然是其中的一部分,具有重要的區別:

  • 對於佈局過程,它們的維度將被視爲零(基本上,它們將被解析爲一個點)
  • 如果他們對其他小部件有限制,他們仍然會受到尊重,但是任何距離都將等於零,被gone的佈局將會視爲一個點

5.維度約束

  1. 設置最小尺寸和我們平常的佈局一樣
android:minWidth 設置佈局的最小寬度
android:minHeight 設置佈局的最小高度
android:maxWidth 設置佈局的最大寬度
android:maxHeight 設置佈局的最大高度
  1. 設置通常佈局有,注意的是當爲0時,視爲match
android:layout_width = warp/match/特定數值
android:layout_height
<Button
    android:id="@+id/button5"
    android:layout_width="120dp"
    android:layout_height="0dp"
    app:layout_constraintDimensionRatio="1:1" //表示匡高比1:1 但是你必須設置一個維度爲0,可以填比也可以填比的值
    android:text="第五個"
    app:layout_constraintVertical_bias="0.7"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
   />

6.鏈

  1. 如果2個控件通過雙向鏈接鏈接在一起,則他們被視爲鏈,鏈的第一個元素被稱爲鏈頭,一般是水平鏈的最左邊不見也是垂直最頂端的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章