我想将一个视图设置为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>