使用 layout_weight 期望子視圖均分寬度/高度,但子視圖擠在一起,權重不生效。
**第二層 (內層) LinearLayout 缺少 android:orientation 屬性**。LinearLayout 預設 orientation="horizontal",但若未顯式聲明,某些情況下權重計算異常;且巢狀佈局時,**父容器與子容器的 orientation 必須明確**,權重才按預期方向分配。
<!-- 外層垂直佈局 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 內層水平佈局:必須顯式聲明 orientation -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"> <!-- 關鍵屬性 -->
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
0dp (寬度用 weight 時 width=0dp,高度用 weight 時 height=0dp)。orientation 與子視圖權重方向一致。layout_weight。