67

我有一个实际上是一个按钮的视图。这是它的 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>

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

v = getActivity().getLayoutInflater().inflate(R.layout.add_new, null);
v.setVisibility(View.GONE);

它消失了,但仍然占据空间。像这样:在此处输入图像描述

此按钮是 中的标题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> 

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

GONE - 此视图是不可见的,并且它不占用任何空间用于布局目的。

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

谢谢,丹尼斯XX

PS 我的列表视图在 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>
4

16 回答 16

52

在我看来,这是一个 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>

只需隐藏 ID 为 LAYOUT_TO_HIDE的LinearLayout,Visible.GONE然后根 FrameLayout 将折叠其高度,从而为您提供一个带有非空白标题的“隐藏”。

于 2013-12-26T14:20:29.033 回答
10

设置layout_width并设置layout_height0您要隐藏项目的位置,通过这样做

//if item's parent layout is LinearLayout, change according to your item xml
holder.itemView.setLayoutParams(new LinearLayout.LayoutParams(0,0));
于 2017-05-28T11:52:20.417 回答
9

此线程中的所有回复都建议使用新的包装器视图,这是有代价的。完全隐藏视图的正确方法是将边距设置为 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);
}
于 2016-08-19T18:29:00.983 回答
4

如果这仍然需要,有一个很好的方法来处理它:

parentView.removeView(childView);

这里有一个例子:

final WhatEverLayout parentView, childView;
parentView = (WhatEverLayout)findViewById(R.id.parentView_xml);
childView =(WhatEverLayout)findViewById(R.id.childView_xml);
parentView.removeView(childView);
于 2013-12-26T14:13:54.123 回答
2

您可以做的是设置一个 if 语句,每当条件将可见性设置为“GONE”时,将视图设置为包装内容并且您的空间将是空闲的,我做到了,它适用于搜索栏

于 2013-10-12T22:15:42.743 回答
2

这是我的解决方案。创建一个名为“special.xml”的新布局文件,将代码复制到文件中。

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

</LinearLayout>

使用条件来膨胀新视图内的空布局。不要使用view.setVisibility(View.GONE); .

if(true){
  view=mInflater.inflate ( R.layout.special,parent, false );
}else{
  view=mInflater.inflate(R.layout.normal,parent,false);
  // The view that you want to be visible
}
于 2015-09-25T06:44:10.253 回答
2

header_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:id="@+id/mHeaderView"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/mNoHelpLayout"
                android:layout_width="match_parent"
                android:layout_height="176dip"
                android:background="#ffffffff"
                android:gravity="center"
             />
       </LinearLayout>
   </LinearLayout>

爪哇代码:

LayoutInflater mInflater =(LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View outerView =  mInflater.inflate(R.layout.header_layout, null);
mHeaderView = (LinearLayout) outerView.findViewById(R.id.mHeaderView);

使用 mHeaderView.setVisiable() 来控制可见而不是 outerView.setVisiable()。这个对我有用。

于 2016-02-26T03:51:33.997 回答
2
If you want to show itemView 

if (ItemBean.isShow()) 
{
   holder.itemView.setVisibility(View.VISIBLE);
   holder.itemView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
 } else
 {
     holder.itemView.setLayoutParams(new LinearLayout.LayoutParams(0, 0));
     holder.itemView.setVisibility(View.GONE);
 }
于 2019-03-16T12:11:38.303 回答
1

我在使用时遇到了类似的问题TransitionManager.beginDelayedTransition(),在Layout Inspector tool我看到视图是Gone,但它仍然占用空间,我认为这是 Android 中的错误,解决方法是执行可见性块View.post(Runnable action)

于 2020-08-02T12:07:55.070 回答
1

对我来说,如果我在下一个运行循环中进行显示/隐藏,它就会起作用。

parentView.post { 
   childViewToHide.visibility = View.GONE
}
于 2020-11-12T21:50:34.473 回答
1

最简单的方法是在孩子可见性改变后强制父视图重新测量自己的高度。
尝试这样的事情:

parentView.setLayoutParams(new LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT));

在这种情况下,父视图是一个LinearLayout

于 2021-06-12T18:42:23.540 回答
0

您可以使用 : viewToHide?.postDelayed({ viewToHide?.visibility = View.GONE }, durationInMillis)

于 2019-09-30T08:22:11.030 回答
0

一个通用且简单的解决方案是避免所有这些代码:

您需要在要隐藏的一组视图上添加容器布局或父布局。它可以是 CardView 或 FrameLayout 等任何东西。

只是它必须是您要隐藏的视图的父级。而且你不会得到所有这些空白。因为它将考虑隐藏整个容器,而不是考虑隐藏单个视图,从而避免空白。

于 2020-03-16T07:49:01.763 回答
0

将视图填充和边距设置为 0,然后将视图可见性设置为消失解决了我的问题。

于 2021-01-15T14:00:52.663 回答
0

在我的情况下,我使用的是动画,在我的动画中,我有标志

android:fillAfter="true"

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">

    <translate
        android:duration="300"
        android:fromYDelta="100%"
        android:toYDelta="0%" />

    
    <alpha
        android:duration="150"
        android:fromAlpha="0"
        android:toAlpha="1" />
</set>

这导致视图被设置为不可见,即使它被设置为GONE.

删除后android:fillAfter="true"我没有更多问题,然后视图被正确设置为GONE.

于 2021-11-05T10:33:09.257 回答
-5

我不认为这是一个错误..只需使用 GONE 使空白空间消失。CSS 做同样的事情。尝试display: blockvisibility: hidden.

于 2015-06-10T20:40:17.247 回答