View.GONE仍然在布局中占用空间

6

我想将一个视图设置为GONE,然后让其他视图占据剩余的空间。

目前,如果我将其设置为GONE,它会在布局中留下它曾经存在的空间,该视图是具有固定高度的viewpager。

到目前为止,我已经阅读了一些资料,得知我需要去掉边距,并且不要为viewpager设置固定高度,因此我尝试做如下操作:

    if (cardsChoice.predictive == true) {

        viewPagerPredicts.setVisibility(View.VISIBLE);
        RelativeLayout.LayoutParams layoutParams = 
        (RelativeLayout.LayoutParams)viewPagerPredicts.getLayoutParams();
        layoutParams.setMargins(8,4,8,0);
        layoutParams.height = R.dimen.predicts_pager_height;
        viewPagerPredicts.setLayoutParams(layoutParams);

    }else{

        viewPagerPredicts.setVisibility(View.GONE);
        RelativeLayout.LayoutParams layoutParams =  
        (RelativeLayout.LayoutParams)viewPagerPredicts.getLayoutParams();
        layoutParams.setMargins(0,0,0,0);
        layoutParams.height = 0;
        viewPagerPredicts.setLayoutParams(layoutParams);
    }

然而这并不起作用 - 视图似乎要么忽略值并匹配父级,要么消失并带走其余的布局。
有人能看出我做错了什么吗?
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout     
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewpagerHolder"
        android:layout_marginTop="64dp"
        android:layout_alignParentBottom="true">

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager2"
            android:layout_width="match_parent"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_height="@dimen/card_pager_height" />

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager_predicts"
            android:layout_width="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="4dp"
            android:layout_height="@dimen/predicts_pager_height"
            android:layout_below="@id/viewpager2" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill"
            android:layout_below="@id/viewpager_predicts"
            android:theme="@style/CustomTabLayoutStyle"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:elevation="4dp"
            android:src="@drawable/ic_playlist_play_white_24dp"
            android:layout_alignBottom="@+id/viewpager2"
            android:layout_alignRight="@+id/viewpager2"
            android:layout_alignEnd="@+id/viewpager2" />


        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:background="@color/windowBackground"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_below="@+id/tabs" />


    </RelativeLayout>

</RelativeLayout>

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize" />

</android.support.design.widget.AppBarLayout>



</android.support.design.widget.CoordinatorLayout>

尝试在XML文件中添加visibility:gone参数并查看事物的行为。我认为边距与此无关,但也许是因为某些项要求该视图,因为它们被对齐为layout_below:viewpager_predicts。 - slorangex
将其设置为 View.INVISIBLE - Raut Darpan
4个回答

2
您提到“其他视图占用剩余空间”,指的是哪些其他视图?我能看到以下内容:
viewpager2 - 它不会占用空间,因为它具有固定高度card_pager_height viewpager - 仅在选项卡受到viewpager_predicts影响时才会占用空间。
选项卡 - 必须在viewpager_predicts消失后受到影响,但您定义了android:layout_below="@id/viewpager_predicts",这在viewpager_predicts消失后将无效。请更正。
还有一件事,在您将可见性设置为GONE之后,不需要更改布局参数,因为这将毫无用处。

非常感谢你所说的一切,都非常有道理,我相信会有所帮助。我将尝试一些东西,看看是否能使其正常工作。我希望选项卡布局在ViewPager2下方,除非布尔值等于x,在这种情况下,它将位于ViewPagerPredicts下方。我是个自学者,可能没有很好地阅读文档,再次感谢。 - Martin Seal

1
  1. May be layout_marginTop="64dp" and android:layout_alignParentBottom="true" confusing you.

  2. Possible reason could be fragment get attached to host activity. set Pager adapter null then set visibility gone.

    if (cardsChoice.predictive == true) { viewPagerPredicts.setAdapter(new YourFragmentAdapter());

        viewPagerPredicts.setVisibility(View.VISIBLE);
        RelativeLayout.LayoutParams layoutParams = 
        (RelativeLayout.LayoutParams)viewPagerPredicts.getLayoutParams();
        layoutParams.setMargins(8,4,8,0);
        layoutParams.height = R.dimen.predicts_pager_height;
        viewPagerPredicts.setLayoutParams(layoutParams);
    
    }else{
    
        viewPagerPredicts.setAdapter(null);
        viewPagerPredicts.setVisibility(View.GONE);
        RelativeLayout.LayoutParams layoutParams =  
        (RelativeLayout.LayoutParams)viewPagerPredicts.getLayoutParams();
        layoutParams.setMargins(0,0,0,0);
        layoutParams.height = 0;
        viewPagerPredicts.setLayoutParams(layoutParams);
    }
    

这看起来是个不错的主意,我今晚会检查一下并在此更新。非常感谢! - Martin Seal

0

我认为你正在寻找这样的东西。 将其放入某个布局文件中,并在预览中检查其行为。在第二个文本视图上设置可见性为gone,你会注意到第一个文本视图占据了剩余的屏幕高度。这可以直接从布局中实现,无需编程设置。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="@string/about_app" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="@string/about_app" />

</LinearLayout>

0

好的,我已经部分解决了我的问题,正如@Girish所指出的那样,这部分是由于我的布局问题(因为没有任何东西占据空白空间),另外一个问题是我在布局消失后尝试编辑布局参数,但我想提到的另一件事是我正在使用一个dimens来设置我的布局高度,这似乎也导致空间为空但保持其空间(像View.INVISIBLE一样),所以为了解决这个问题,我将其包装在另一个视图中,并将该视图设置为View.GONE,这似乎非常有效,所以我的最终代码看起来像这样

if (cardsChoice.predictive) {

        linearLayout.setVisibility(View.VISIBLE);
        viewPagerPredicts.setVisibility(View.VISIBLE);
        LinearLayout.LayoutParams layoutParams = 
        (LinearLayout.LayoutParams)viewPagerPredicts.getLayoutParams();
        layoutParams.setMargins(8,4,8,0);
        viewPagerPredicts.setLayoutParams(layoutParams);

    }else{

        viewPagerPredicts.setAdapter(null);
        LinearLayout.LayoutParams layoutParams = 
        (LinearLayout.LayoutParams)viewPagerPredicts.getLayoutParams();
        layoutParams.setMargins(0,0,0,0);
        viewPagerPredicts.setLayoutParams(layoutParams);
        viewPagerPredicts.setVisibility(View.GONE);
        linearLayout.setVisibility(View.GONE);
    } 

我的 XML 文件看起来像这样

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout   
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/viewpagerHolder"
        android:layout_marginTop="64dp">

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager2"
            android:layout_width="match_parent"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_height="@dimen/card_pager_height" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/predictsHolder"
            android:orientation="vertical">

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager_predicts"
            android:layout_width="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="4dp"
            android:layout_height="@dimen/predicts_pager_height" />

        </LinearLayout>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill"
            android:theme="@style/CustomTabLayoutStyle" />

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:background="@color/windowBackground"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
             />


    </LinearLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:elevation="4dp"
    android:src="@drawable/ic_playlist_play_white_24dp" />

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize" />

</android.support.design.widget.AppBarLayout>

感谢你们的帮助,你们俩都应该得到一块饼干和回答的分数,但我只能接受一个,抱歉。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接