You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中setVisibility(View.GONE)表现为INVISIBLE问题求助

Fix for ImageView Still Taking Space When Set to View.GONE

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:

  1. Add android:layout_weight="0" to the ImageView to confirm it doesn't claim extra space
  2. Change the text container's layout_width to 0dp and add android: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_weight attribute tells the parent LinearLayout how 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 to weight="1").
  • When the ImageView is set to View.GONE, it's completely excluded from layout calculations. The text container, with width="0dp" and weight="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 returning false when 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:17:41