TShopping

標題: Android ConstraintLayout 基本介紹 [打印本頁]

作者: woff    時間: 2020-7-17 15:05
標題: Android ConstraintLayout 基本介紹
ConstraintLayout為一個Support Library,可以用在Android2.3(API 9)以上的版本。
之前在設計畫面的時候都是使用LinearLayout或RelativeLayout,但有時候在比較複雜的畫面,我們疊太多層的Layout會耗比較多的資源,而ConstraintLayout將Layout平面化,減少資源浪費。

增加Library到Project
  1. dependencies {
  2.         compile 'com.android.support.constraint:constraint-layout:1.0.2'
  3.     }
複製代碼
介紹相對定位屬性



  1. <Button
  2.     android:text="Button"
  3.     android:layout_width="wrap_content"
  4.     android:layout_height="wrap_content"
  5.     android:id="@+id/button"
  6.     app:layout_constraintBottom_toBottomOf="parent"
  7.     app:layout_constraintRight_toRightOf="parent"
  8.     app:layout_constraintLeft_toLeftOf="parent"
  9.     app:layout_constraintTop_toTopOf="parent" >
複製代碼

元件在另一個元件的上方


  1. <Button
  2.     android:text="Button"
  3.     android:layout_width="wrap_content"
  4.     android:layout_height="wrap_content"
  5.     android:id="@+id/button2"
  6.     android:layout_marginBottom="48dp"
  7.     app:layout_constraintBottom_toTopOf="@+id/button"
  8.     app:layout_constraintRight_toRightOf="@+id/button"
  9.     app:layout_constraintLeft_toLeftOf="@+id/button">
複製代碼




  1. app:layout_constraintRight_toLeftOf = "@+id/B"
複製代碼


文章出處







歡迎光臨 TShopping (http://www.tshopping.com.tw/) Powered by Discuz! X3.2