AppBarLayout在setVisibility(View.GONE)后仍占用空间

24

我想动态添加一个视图,这个视图会占据屏幕的高度,因此我希望隐藏我的AppBarLayout。为了实现这一点,我想通过将AppBarLayout的可见性设置为GONE来暂时删除一个视图。视图虽然不可见,但仍然占据屏幕上的空间(屏幕高度的一半)。

我的XML代码:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinatorFriendProfil"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#81D4FA"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/fProfilAppBar"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/fProfilCollapsing"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#B3E5FC"
        android:gravity="center"
        android:orientation="vertical"
        app:contentScrim="#03A9F4"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <LinearLayout
            android:id="@+id/fProfilUserInfo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">

            <com.neden.neden.RoundedImageView
                android:id="@+id/ivFriendPicture"
                android:layout_width="150sp"
                android:layout_height="150sp"
                android:layout_gravity="center"
                android:fitsSystemWindows="true"
                android:src="@drawable/photo"
                app:border_color="#64B5F6"
                app:border_width="4dp"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.7" />

            <TextView
                android:id="@+id/fProfilName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="15pt" />

        </LinearLayout>

        <android.support.v7.widget.Toolbar
            android:id="@+id/fProfilToolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:layout_scrollFlags="scroll|enterAlways" />

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

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

<FrameLayout
    android:id="@+id/fProfilContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
4个回答

32

你的问题是由这行代码引起的

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

你需要在CoordinatorLayout上编程设置,而不是实际包含该字段的容器片段。

以下是具体步骤

    mContainer = (FrameLayout) findViewById(R.id.main_content_container);

    CoordinatorLayout.LayoutParams params =
            (CoordinatorLayout.LayoutParams) mContainer.getLayoutParams();
    params.setBehavior(null);

    mContainer.requestLayout();

如果你想再次让它滚动

   params.setBehavior(new AppBarLayout.ScrollingViewBehavior());

在 Stack Overflow 上尝试了太多解决方案后,这个答案对我有用。 - Pankaj Kumar

7
尝试将AppBarLayout的高度设为零。
AppBarLayout appBar=(AppBarLayout)findViewById(R.id.fProfilAppBar);
LinearLayout.LayoutParams params=appBar.getLayoutParams();
params.height=0;
appBar.setLayoutParams(params);

它可以工作,但是当我这样做时,我的FrameLayout的layout_height不会采用match_parent。 - Okn
你说的“layout_height不能使用match_parent”是什么意思?它不适配屏幕吗? - mohax
抱歉我的英文不好。我想让FrameLayout占据整个屏幕的高度,但是当我设置appBarLayout时,FrameLayout位于屏幕顶部而不是底部(就像一个margin_bottom,但我在代码中看不到任何margin)。为了简化,我想要android:layout_height:"match_parent"。 - Okn
这可能是由于 android:fitsSystemWindows="true" 引起的。尝试从 CoordinatorLayout/FrameLayout 中添加/删除它。或者只需向 FrameLayout 添加 layout_marginTop 即可。 - mohax
我尝试了所有的组合,但是android:fitsSystemWindows="true"无法正常工作。 - Okn
在将AppBarLayout的高度设置为零后,尝试将FrameLayout的高度设置为LayoutParams.MATCH_PARENT。对于FrameLayout,请使用FrameLayout.LayoutParams - mohax

0

我已经尝试过这些方法,但仍然无法正常工作。更好的解决方案是动态地显示和隐藏CollapsingToolbar并设置AppBarLayout参数。

每当您显示/隐藏CollapsingToolbarLayout时,请执行此操作。

mAppBar.setLayoutParams(**new CoordinatorLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT)**);

0
我通过将一切从CollapsingToolbarLayout(CTL)移至Toolbar来解决此问题,使得Toolbar成为CTL的唯一子代。然后,我将CTL的高度设置为wrap_content,并在代码中使用了toolbar.setVisibility(View.GONE)。请注意,您可能需要更改Toolbar内部的布局,添加一些包装器,如RelativeLayout或其他内容。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/activity_main_root_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/default_page_background_color"
        android:orientation="vertical"
        tools:context=".home.ui.MainActivity">

        <android.support.design.widget.CoordinatorLayout
            android:id="@+id/page_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/default_page_background_color"
            android:fitsSystemWindows="true"
            app:layout_constraintBottom_toTopOf="@id/home_nav_tabs"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <android.support.design.widget.AppBarLayout
                android:id="@+id/appbar_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="false"
                android:orientation="vertical"
                android:theme="@style/AppTheme.AppBarOverlay"
                app:elevation="0dp">

                <android.support.design.widget.CollapsingToolbarLayout
                    android:id="@+id/collapsing_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:contentScrim="?attr/colorPrimary"
                    app:layout_scrollFlags="scroll|enterAlways|snap"
                    app:scrimAnimationDuration="300">

                    <android.support.v7.widget.Toolbar
                        android:id="@+id/toolbar"
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        app:buttonGravity="top"
                        app:layout_collapseMode="none"
                        app:popupTheme="@style/AppTheme.PopupOverlay">

                        ...

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