<EditText
android:id="@+id/input_text"
android:layout_width="0dp" <!-- 錯誤:寬度為 0 -->
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Type something here"
android:maxLines="2" />
報錯提示:this will make the view invisible, probably intended for layout_height。
`layout_width="0dp"` + `layout_weight="1"` 僅在**水平 LinearLayout** 中有效(表示「寬度由權重分配,初始為 0」)。
但父容器若為 **垂直 LinearLayout**,則應設置 layout_height="0dp" + `layout_weight="1"` 實現高度自適應分配。
代碼中同時設置 `layout_width="0dp"` (水平權重) 和 `layout_height="wrap_content"` (固定高度),導致寬度為 0,View 不可見。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 其他固定高度 View -->
<TextView ... />
<EditText
android:id="@+id/input_text"
android:layout_width="match_parent" <!-- 寬度撐滿 -->
android:layout_height="0dp" <!-- 高度初始 0,由 weight 分配 -->
android:layout_weight="1"
android:hint="Type something here"
android:maxLines="2" />
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send" />
</LinearLayout>
<androidx.constraintlayout.widget.ConstraintLayout ...>
<EditText
android:id="@+id/input_text"
android:layout_width="0dp" <!-- match_constraint -->
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/above_view"
app:layout_constraintBottom_toTopOf="@id/send_btn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/send_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
ConstraintLayout 無需嵌套權重,性能更優、佈局更靈活,**新項目首選**。
記住:「權重配 0dp,方向要對」,佈局不再踩坑。