首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

Android 佈局四個 RelativeLayout 水平等距分配:LinearLayout 權重與 ConstraintLayout 方案

     編輯:程式設計綜合問答

需求

希望在 Android 頁面底部放置四個 RelativeLayout,水平方向平均分配空間。

建議方案一:LinearLayout 權重

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="96dp"
    android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>

方案二:ConstraintLayout 鏈條

使用 ConstraintLayout 的水平鏈條(Horizontal Chain)並設定 layout_constraintHorizontal_weight,也能達到等距分配。

注意事項

  • 子項寬度建議設為 0dp(MATCH_CONSTRAINT),搭配 layout_weight 使用。
  • 避免使用 wrap_content,否則無法平均分配。
  • 原始 XML 結構不完整,建議以 LinearLayout 或 ConstraintLayout 作為根容器。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved