setVisibility(GONE)视图变为不可见但仍占用空间

Den*_*nis 53 android android-listview android-view

我有一个视图,实际上是一个按钮.这是它的XML布局(add_new.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="horizontal">
    <Button
        android:id="@+id/buttonNew"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/bText" 
        android:onClick="addNew"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

当我将其可见性设置为GONE时

v = getActivity().getLayoutInflater().inflate(R.layout.add_new, null);
v.setVisibility(View.GONE);
Run Code Online (Sandbox Code Playgroud)

它消失但仍占据空间.像这样:在此输入图像描述

这个按钮是一个标题,ListView由这个xml定义:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/porno" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="30dp"
        android:layout_height="40dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="4dp"
        android:src="@drawable/ic_launcher">
    </ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="20dp" >
    </TextView>

</LinearLayout> 
Run Code Online (Sandbox Code Playgroud)

当其可见性设置为GONE时,我不希望它占用额外的列表项.正如文档中所述.

GONE - 此视图不可见,并且不需要任何空间用于布局.

关于如何使它不占用空间的任何想法?

谢谢,Dennis xx

PS我的listview位于FoldersFragment内部,ListFragment这里是我的MainActivity的xml,其中提供了FoldersFragment

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/foldersFragment"
        android:layout_width="200dip"
        android:layout_height="match_parent"
        class="com.example.fragments.FoldersFragment" >
    </fragment>

    <fragment
        android:id="@+id/detailFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.example.fragments.DetailFragment" >
    </fragment>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

non*_*oni 46

在我看来,这是一个Android错误,我们只是解决了这个问题:

<FrameLayout android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout android:id="@+id/layout_to_hide"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content">
         //Put here your views
    </LinearLayout>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

只是隐藏的LinearLayout id为LAYOUT_TO_HIDEVisible.GONE再根的FrameLayout会崩溃的高度给你一个"隐藏"与非空白空间标题.

  • 我在 2021 年也面临这个问题..有什么更新吗? (6认同)
  • 怎么到了2021年了这还是一个问题 (4认同)
  • 你好呀。您还没有找到更好的解决方案吗?这真的是一个错误吗?我有同样的问题 (3认同)

Jag*_*uar 8

这个帖子中的所有回复都建议一个新的包装器视图,这需要付出代价.完全隐藏视图的正确方法是将边距设置为0,同时将可见性设置为GONE.在此代码示例中,cardView是我试图隐藏的视图.cardView的直接父是RecyclerView,这就是我们使用RecyclerView.LayoutParams的原因 - 记得用正确的布局参数替换.

if (cardView.getVisibility() != GONE) {
    cardView.setVisibility(GONE);
    RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) cardView.getLayoutParams();
    layoutParams.setMargins(0, 0, 0, 0);
    cardView.setLayoutParams(layoutParams);
}
Run Code Online (Sandbox Code Playgroud)


Asa*_*sad 8

设置layout_widthlayout_height0要隐藏的项目,这样做

//if item's parent layout is LinearLayout, change according to your item xml
holder.itemView.setLayoutParams(new LinearLayout.LayoutParams(0,0));
Run Code Online (Sandbox Code Playgroud)