ConstraintLayout 簡單實用
ConstraintLayout(約束佈局)是在2016的Google I/O大會上釋出的。
- 支援Android 2.3(api 9 +)
- 單獨的依賴包
- ConstraintLayout的實現基於食火鳥演算法(Cassowary Algorithm),它是一個高效的約束解決方案。
- 優勢:可以解決佈局過度巢狀,效率低下,還可以實現複雜動畫。

image.png
<?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"> <!--layout_constraintXX_toYYOfxx 代表當前元件 YY代表對方元件--> <!--layout_constraintDimensionRatio比列 --> <!-- android:layout_width="0dp"當文字資訊過長的時候,文字資訊會覆蓋圖片元件,設定為0dp 就可以解決了 --> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="16dp" app:layout_constraintDimensionRatio="1:1" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.03" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@drawable/ic_launcher_background" /> <TextView android:id="@+id/textView" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginTop="16dp" android:layout_marginStart="16dp" android:text="TextView" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@id/imageView" app:layout_constraintTop_toTopOf="parent" tools:text="HELLO___))))))))))))))))))))))))))))) )))666))" /> <TextView android:id="@+id/textView2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginTop="16dp" android:layout_marginStart="16dp" android:text="TextView" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@id/imageView" app:layout_constraintTop_toBottomOf="@+id/textView" tools:text="XXXXXXXYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYX" /> </android.support.constraint.ConstraintLayout>
注意: 提出一個問題,如果我想讓第二行的文字資訊和圖片底部對齊怎麼實現呢?歡迎大家回覆我。