Android布局中隐藏EditText后仍留空白的原因咨询
问题
我正在创建动态表单输入组件,部分字段需隐藏。但在onCreateView方法中调用binding.vendorinvoice.setVisibility(View.INVISIBLE)隐藏EditText后,布局仍显示空白空间,我希望隐藏字段后其余字段能自动上移。以下是我的布局XML代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/ic_launcher_background" tools:context=".activity.NewReceipt.NewReceiptReceiptHeader"> <!-- TODO: Update blank fragment layout --> <androidx.cardview.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="6dp" android:elevation="3dp" android:background="@color/white" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <EditText android:id="@+id/projectnumber" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:hint="Project Number" /> <EditText android:id="@+id/projectsequence" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:hint="Project sequence number" /> </LinearLayout> </androidx.cardview.widget.CardView> <androidx.cardview.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="6dp" android:elevation="3dp" android:background="@color/white"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <EditText android:id="@+id/vendor" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:hint="Vendor" /> <EditText android:id="@+id/vendorinvoice" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:hint="Vendor Invoice number" /> </LinearLayout> </androidx.cardview.widget.CardView> <androidx.cardview.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="6dp" android:elevation="3dp" android:background="@color/white"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <EditText android:id="@+id/carrier" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:hint="Carrier" /> <EditText android:id="@+id/carriertrackingnumber" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:hint="Carrier tracking number" /> </LinearLayout> </androidx.cardview.widget.CardView> </LinearLayout>
截图显示Vendor下方存在空白空间,请问该空白为何存在?
原因及解决方案
空白存在的原因
View.INVISIBLE仅会让控件不可见,但它仍然保留原有的布局空间,所以Vendor下方会留下vendorinvoice EditText原本占据的位置,表现为空白区域。
解决办法
- 替换为
View.GONE:将代码改为binding.vendorinvoice.setVisibility(View.GONE)。View.GONE会让控件完全从布局中移除,不再占用任何空间,后续的布局元素会自动上移填补空缺。 - 初始状态优化:如果该字段默认需要隐藏,可以直接在XML中给
vendorinvoice设置android:visibility="gone",需要显示时再动态改为View.VISIBLE,避免初始布局出现空白。
内容的提问来源于stack exchange,提问作者btorkelson
相关产品推荐
相关产品推荐

