Android布局问题:如何让两个TextInput横向填充屏幕并保留间距?
解决方案:LinearLayout实现两个TextInput横向平分宽度并留间距
修改后的布局代码如下:
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginStart="16dp" android:layout_marginEnd="16dp"> <com.google.android.material.textfield.TextInputLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginEnd="8dp"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TEST 1" /> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.textfield.TextInputLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TEST 2" /> </com.google.android.material.textfield.TextInputLayout> </LinearLayout>
关键修改说明:
- 将两个
TextInputLayout的android:layout_width设为0dp,同时添加android:layout_weight="1":LinearLayout会根据weight值分配剩余宽度,两个weight均为1时会平分可用空间。 - 给第一个
TextInputLayout添加android:layout_marginEnd="8dp":在两个输入框之间设置固定间距,可根据需求调整数值(比如16dp)。 - 保持
TextInputEditText的android:layout_width="match_parent":确保输入框完全填满所在的TextInputLayout宽度。
当前显示效果:两个TextInput仅包裹自身内容宽度,未填充屏幕横向空间。
期望显示效果:两个TextInput横向平分屏幕宽度,中间保留指定间距,整体填满父布局的横向空间。
内容的提问来源于stack exchange,提问作者user19729496
相关产品推荐
相关产品推荐

