1 回答

TA貢獻2051條經驗 獲得超10個贊
TL;DR?這是另一種方法?ConstraintLayout
,不是針對每種屏幕尺寸使用單一布局
您可以使用ConstraintLayout支持不同的屏幕尺寸:
ConstraintLayout 允許您創建具有平面視圖層次結構(無嵌套視圖組)的大型復雜布局。它與 RelativeLayout 相似,因為所有視圖都是根據兄弟視圖和父布局之間的關系進行布局,但它比 RelativeLayout 更靈活,并且更容易與 Android Studio 的布局編輯器一起使用。
您可以使用這些屬性以百分比指定視圖大?。?/p>
app:layout_constraintWidth_percent=".5" app:layout_constraintHeight_percent=".5"
例如,單個按鈕的高度和寬度都等于屏幕的 50%:
<?xml?version="1.0"?encoding="utf-8"?> <androidx.constraintlayout.widget.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"> <Button ????android:layout_width="0dp" ????android:layout_height="0dp" ????app:layout_constraintBottom_toBottomOf="parent" ????app:layout_constraintEnd_toEndOf="parent" ????app:layout_constraintStart_toStartOf="parent" ????app:layout_constraintTop_toTopOf="parent" ????app:layout_constraintWidth_percent=".5" ????app:layout_constraintHeight_percent=".5"/> </androidx.constraintlayout.widget.ConstraintLayout>
它看起來像這樣:
我可以推薦使用ConstraintLayout?with?guidelines和Chains來支持不同的屏幕尺寸(除了我上面提到的 -app:layout_constraintWidth_percent
和app:layout_constraintHeight_percent
)。
乍一看,這可能需要大量工作,有些人可能想知道這是否真的值得付出努力,但這就是為什么我相信 ConstraintLayout 是構建 UI 的正確方法:
它真的很用戶友好。
ConstraintLayout 非常簡單易學。
一旦你學會了它,你會發現你節省了大量的開發時間,因為制作你的 UI 非常簡單。
約束布局旨在支持不同的屏幕尺寸,因此無需為每個屏幕尺寸構建布局(這也與之前的優勢相關——節省開發時間)。
添加回答
舉報