本文介绍了setVisibility(GONE) 视图变得不可见但仍然占用空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实际上是一个按钮的视图.这是它的 XML 布局 (add_new.xml)

I've got a view that is effectively is a button. Here is its XML layout (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 时

When I set its visibility to GONE like this

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

它消失了,但仍然占据空间.像这样:

it disappears but still occupies space. Like this:

这个按钮是ListView中的一个header,由这个xml定义:

This button is a header in the ListView, which is defined by this 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 时,我不希望它占用额外的列表项.正如文档中所述.

And I dont want it to occupy an additional list item when its visibility is set to GONE. As it is stated in the documentation.

GONE - 这个视图是不可见的,它不占用任何空间布局目的.

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

Any ideas on how to make it NOT occupy space?

谢谢,丹尼斯 xx

附言我的列表视图位于 FoldersFragment ListFragment 内,这是我的 MainActivity 的 xml,其中显示了 FoldersFragment

P.S. My listview is inside of a FoldersFragment ListFragmentand here is the xml of my MainActivity where FoldersFragment is presented

<?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>

推荐答案

在我看来,这是一个 Android 错误,我们只是通过这样做来解决这个问题:

This is an Android bug in my opinion, we just fix this issue doing this:

<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>

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

Just hide LinearLayout with id LAYOUT_TO_HIDE with Visible.GONE and then root FrameLayout will collapse its height giving you a "hidden" with non-blank-space header.

这篇关于setVisibility(GONE) 视图变得不可见但仍然占用空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 08:58
查看更多