ScrollView内TextInputLayout的layout_weight失效问题求助
问题解决:横屏时TextInputLayout忽略layout_weight的问题
问题场景
布局包含两个设置了layout_weight的TextInputLayout和一个TableLayout,嵌套在ScrollView内。竖屏时文本框按比例分配高度,内容超出时内部滚动正常;但旋转到横屏后,文本框会被内容撑大,完全忽略了layout_weight的设置。
原因分析
问题出在内部的LinearLayout上:它的android:layout_height设为wrap_content,结合ScrollView的fillViewport="true",横屏时LinearLayout会根据子控件的内容高度自适应,导致layout_weight的比例规则失效——layout_weight只有在父布局高度为固定值(match_parent)时,才能正确按比例分配剩余空间。
解决步骤
只需要修改内部LinearLayout的高度属性:
- 将
android:layout_height="wrap_content"改为android:layout_height="match_parent"
修改后的完整布局代码:
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent" android:fillViewport="true"> <LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 此处为修改点 --> <com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" android:layout_marginTop="10dp" android:layout_marginRight="10dp" android:layout_marginLeft="10dp" android:layout_marginBottom="10dp" android:hint="@string/code_hint" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="top" android:scrollbars="vertical" android:id="@+id/code_field"/> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:layout_marginTop="10dp" android:layout_marginRight="10dp" android:layout_marginLeft="10dp" android:layout_marginBottom="10dp" android:hint="@string/input_hint" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="top" android:id="@+id/input_field"/> </com.google.android.material.textfield.TextInputLayout> <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginBottom="50dp" android:layout_marginRight="10dp" android:layout_marginLeft="10dp"> <TableRow android:layout_height="wrap_content" android:layout_width="match_parent" android:gravity="center"> <com.google.android.material.button.MaterialButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/run_button" android:text="@string/run" app:icon="@drawable/ic_baseline_play_arrow_24"/> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" style="?android:attr/buttonBarStyle"> <com.google.android.material.button.MaterialButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" android:id="@+id/plus_button" android:text="@string/plus" android:textSize="20sp" style="?android:attr/buttonBarButtonStyle"/> <com.google.android.material.button.MaterialButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" android:id="@+id/minus_button" android:text="@string/minus" android:textSize="20sp" style="?android:attr/buttonBarButtonStyle"/> <com.google.android.material.button.MaterialButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" android:id="@+id/left_shift_button" android:text="@string/left_shift" android:textSize="20sp" style="?android:attr/buttonBarButtonStyle"/> <com.google.android.material.button.MaterialButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" android:id="@+id/right_shift_button" android:text="@string/right_shift" android:textSize="20sp" style="?android:attr/buttonBarButtonStyle"/> <com.google.android.material.button.MaterialButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" android:id="@+id/backspace_button" app:icon="@drawable/ic_baseline_backspace_24" style="?android:attr/buttonBarButtonStyle"/> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" style="?android:attr/buttonBarStyle"> <com.google.android.material.button.MaterialButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" android:id="@+id/start_loop_button" android:text="@string/start_loop" android:textSize="20sp" style="?android:attr/buttonBarButtonStyle"/> <com.google.android.material.button.MaterialButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" android:id="@+id/end_loop_button" android:text="@string/end_loop" android:textSize="20sp" style="?android:attr/buttonBarButtonStyle"/> <com.google.android.material.button.MaterialButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" android:id="@+id/dot_button" android:text="@string/dot" android:textSize="20sp" style="?android:attr/buttonBarButtonStyle"/> <com.google.android.material.button.MaterialButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" android:id="@+id/comma_button" android:text="@string/comma" android:textSize="20sp" style="?android:attr/buttonBarButtonStyle"/> </TableRow> </TableLayout> </LinearLayout> </ScrollView>
效果验证
修改后,无论横竖屏,两个TextInputLayout都会按照2:1的比例分配高度,内容超出时内部滚动,layout_weight规则完全生效,TableLayout也会正常显示在文本框下方。
内容的提问来源于stack exchange,提问作者Sandeep Tadepalli
相关产品推荐
相关产品推荐

