Android中setVisibility(View.GONE)表现为INVISIBLE问题求助
Hey there! Let's sort out why your ImageView is behaving like it's set to View.INVISIBLE instead of View.GONE—even after you explicitly set it to GONE.
The Root Cause
Your layout uses a horizontal LinearLayout where the ImageView has a fixed width (@dimen/list_item_height), and the right-side text container uses layout_width="match_parent". While View.GONE should remove the view entirely from the layout flow, the way LinearLayout calculates space here can leave an empty spot where the ImageView was. The fix lies in adjusting how the layout allocates space between the two child views.
The Solution
Modify your layout XML with these two key changes to ensure proper space distribution:
- Add
android:layout_weight="0"to the ImageView to confirm it doesn't claim extra space - Change the text container's
layout_widthto0dpand addandroid:layout_weight="1"so it fills all remaining space when the ImageView is gone
Here's the updated layout code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools" android:minHeight="@dimen/list_item_height" android:background="@color/tan_background"> <ImageView android:layout_width="@dimen/list_item_height" android:layout_height="@dimen/list_item_height" android:id="@+id/image" android:layout_weight="0" /> <LinearLayout android:layout_width="0dp" android:layout_height="88dp" android:orientation="vertical" android:paddingLeft="16dp" android:id="@+id/text_container" android:gravity="center_vertical" android:layout_weight="1"> <TextView tools:text="one" android:id="@+id/default_textview" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:paddingLeft="9dp" android:textColor="@android:color/white" android:gravity="bottom" android:textStyle="bold" android:textAppearance="?android:textAppearanceMedium"/> <TextView android:gravity="top" android:textColor="@android:color/white" android:textAppearance="?android:textAppearanceMedium" android:layout_width="match_parent" android:id="@+id/miwok_textview" android:layout_height="0dp" android:layout_weight="1" android:paddingLeft="9dp" tools:text="lutti" /> </LinearLayout> </LinearLayout>
Why This Works
- The
layout_weightattribute tells the parentLinearLayouthow to split up leftover space. When the ImageView is visible, its fixed width is accounted for first, and the text container gets all remaining space (thanks toweight="1"). - When the ImageView is set to
View.GONE, it's completely excluded from layout calculations. The text container, withwidth="0dp"andweight="1", will expand to fill the entire width of the parent LinearLayout—no empty space left behind.
Quick Double-Check
Also, make sure your Java logic is working as expected:
- Verify that
currentword.hasImg()is correctly returningfalsewhen you expect the ImageView to be hidden - Ensure you're updating the View's visibility on the UI thread (if you're doing this from a background task, use
runOnUiThread()or a Handler to trigger the change)
内容的提问来源于stack exchange,提问作者subham paliwal

