ConstraintLayout
是Google提供的一種ViewGrop
,由於它不用像LinearLayout
那樣嵌套的方式實現複雜的Layout,使得ConstraintLayout
更加易寫易讀,還能讓App的性能有所提升(由於少了層級嵌套,因此繪製時的性能損耗也會減小)。android
ConstraintLayout
如其名字,它是經過設置顯示限制來決定View的顯示位置。git
正常狀況下,新建一個工程時會自動加入ConstraintLayout
的庫,若是沒有則須要手動加入。github
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
複製代碼
接下來須要把layout文件中的根ViewGroup
設置成ConstraintLayout
。app
<androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent">
......
</androidx.constraintlayout.widget.ConstraintLayout>
複製代碼
介紹一下決定View
位置的幾種ConstraintLayout
的限制條件。ide
app:layout_constraintTop_toTopOf
//該View的上部與指定的View上部位置一致app:layout_constraintTop_toBottomOf
//該View的上部與指定的View下部位置一致app:layout_constraintBottom_toBottomOf
app:layout_constraintBottom_toTopOf
app:layout_constraintStart_toStartOf
app:layout_constraintStart_toEndOf
app:layout_constraintEnd_toEndOf
app:layout_constraintEnd_toStartOf
須要注意的是除了設置View
的ID
,還能夠設置parent
。parent
是指顯示界面。 還有好比同時設置左右限制,則View會居中,就像兩端被繩子拉緊居中似的。post
若是想要左側或者右側傾斜,則須要加入權重。性能
app:layout_constraintHorizontal_bias="0.2"
複製代碼
還有若是要設置View的margin須要用下面的方法。gradle
android:layout_marginTop
android:layout_marginButtom
android:layout_marginStart
android:layout_marginEnd
舉一個例子,你們應該很快就會理解了。動畫
<TextView android:id="@+id/textView10" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="50dp" android:text="TextView10" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.2" app:layout_constraintStart_toStartOf="parent" />
複製代碼
有時候咱們會有根據多個View中最右端的位置,決定另外一個View的位置。若是使用ConstraintLayout
的Barrier
就容易實現。ui
首先增長一個Barrier
,而且設置以下的設置。
須要決定要把Barrier
放置在相對於View的哪一個位置。根據上述需求:
app:barrierDirection="end"
複製代碼
除了end
,還有start
,top
,bottom
。
還須要設置對限制起做用的引用ID。若是咱們是須要根據兩個TextView中最右端的位置,來決定另外一View的位置時,能夠寫成以下的代碼。
app:constraint_referenced_ids="textView1,textView2"
複製代碼
<androidx.constraintlayout.widget.Barrier android:id="@+id/barrier" android:layout_width="wrap_content" android:layout_height="wrap_content" app:barrierDirection="end" app:constraint_referenced_ids="textView1,textView2" />
複製代碼
若是在一個View中有不少組件須要設置同樣的margin的時候可使用GuideLine
,使其日後的總體修改更容易,代碼更加簡潔。
GuideLine
正如它的名字,它是一個參考線,但它是隱形的。GuideLine
自己是不會顯示在View中的,它只是做爲參考線,統一各個View的margin。
咱們須要設置參考線是縱向仍是橫向的。以下代碼。
android:orientation="vertical"
複製代碼
vertical
是縱向,horizontal
是橫向。
經過使用基本限制條件來設置參考線的位置。但這裏有一個特別值得注意的一點是,margin的設置是不能用普通的限制條件,須要用其專門的margin限制條件。
margin的限制條件:
app:layout_constraintGuide_end
app:layout_constraintGuide_start
app:layout_constraintGuide_top
app:layout_constraintGuide_bottom
下面的代碼的意思是,把參考線設置在離界面右端的30dp的位置。
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintGuide_end="30dp"
複製代碼
剩下的就是把View的位置限制條件設置成GuideLine
便可。
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World1!" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toEndOf="@id/guideline1" />
複製代碼
<androidx.constraintlayout.widget.Guideline android:id="@+id/guideline1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintStart_toStartOf="parent" app:layout_constraintGuide_begin="30dp" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World1!" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toEndOf="@id/guideline1" />
複製代碼
根據上面的敘述,對View設置左右的限制時,該View會居中顯示。可是若是兩個View想要貼在一塊兒顯示,就須要用到ChainStyle
。
ChainStyle
一共有三種類型。
固然根據方向分爲縱向和橫向的ChainStyle
。
使用ChainStyle
時有一個注意點是,須要對View的兩側都要設置限制,若是缺乏限制條件,ChainStyle
就不會起做用。
spread_inside
是兩邊View貼近邊緣,中間居中。
spread
是全部View居中。
packed
是全部的View都貼在一塊兒。
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="150dp" android:text="TextView1" app:layout_constraintEnd_toStartOf="@id/textView2" app:layout_constraintHorizontal_chainStyle="packed" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
<TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="150dp" android:text="TextView2" app:layout_constraintEnd_toStartOf="@id/textView3" app:layout_constraintHorizontal_chainStyle="packed" app:layout_constraintStart_toEndOf="@id/textView1" app:layout_constraintTop_toTopOf="parent" />
<TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="150dp" android:text="TextView3" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_chainStyle="packed" app:layout_constraintStart_toEndOf="@id/textView2" app:layout_constraintTop_toTopOf="parent" />
複製代碼
除了上述內容,還有不少關於ConstraintLayout
的其餘內容,由於篇幅有限就再也不介紹了。
GitHub: github.com/HyejeanMOON…
Google的MergeAdapter的使用
Paging在Android中的應用
Android WorkManager的使用 android中的Transition動畫的使用