Android 隐藏AppBarLayout并将其空间提供给剩余视图

Android 隐藏AppBarLayout并将其空间提供给剩余视图,android,android-layout,android-design-library,android-coordinatorlayout,Android,Android Layout,Android Design Library,Android Coordinatorlayout,使用新的设计库,我有一个非常标准的布局: <AppBarLayout> <CollapsingToolbarLayout> <ImageView/> <Toolbar/> </CollapsingToolbarLayout> </AppBarLayout> <android.support.v4.widget.NestedScrollView/> <!--

使用新的设计库,我有一个非常标准的布局:

<AppBarLayout>
    <CollapsingToolbarLayout>
        <ImageView/>
        <Toolbar/>
    </CollapsingToolbarLayout>
</AppBarLayout>

<android.support.v4.widget.NestedScrollView/> <!-- content here -->

要禁用折叠行为(效果良好),请执行以下操作:

@Override
public void hide() {
    final AppBarLayout layout = (AppBarLayout) findViewById(R.id.appbar);

    layout.animate().translationY(-layout.getHeight())
            .setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    layout.setVisibility(View.GONE);
                }
            }).start();
}
我将AppBarLayout转换到顶部(工作平稳),动画集的末尾是查看视图的可见性

问题 在动画结束时,无论我是否也将可见性设置为“已消失”,我都无法获得以前由
AppBarLayout
占用的空间。我的NestedScrollView仍然被限制在屏幕的下半部分,好像AppBarLayout仍然在那里(现在不是)。我怎样才能修好它

隐藏前:

隐藏后(AppBar被转换到顶部):

如您所见,顶部空间是空的,无法访问。滚动视图在之前的页边距内滚动,就好像可见性变化不是由
坐标布局测量的

  • 我尝试调用协调器.requestLayout()
,但没有成功

  • 我还尝试将AppBarLayout设置为NestedScrollView的
    app:anchor
    ,但这会把事情搞砸——scroll view甚至在隐藏之前就占据了整个屏幕

  • 我想在进入这个隐藏的AppBar模式时,在滚动视图上设置一个自定义的
    行为
    ,但我无法开始


  • 是的,这看起来像一个bug,我为我的应用程序解决了这个问题,将appbar高度设置为0:

    android.support.design.widget.AppBarLayout appbar = (android.support.design.widget.AppBarLayout) findViewById(R.id.appbar);
    
    CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)appbar.getLayoutParams();
    
    lp.height = 0;
    
    appbar.setLayoutParams(lp);
    

    以下内容也适用

    appBarLayout.setExpanded(false, false);
    appBarLayout.setVisibility(View.GONE);
    

    这对我有用。只需打开/关闭appbar

    private boolean hide = true;
    public void toggleAppBar() {
    
        // Calculate ActionBar height
        TypedValue tv = new TypedValue();
        int actionBarHeight = 0;
        if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
            actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
        }
    
    
        CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)appBarLayout.getLayoutParams();
        lp.height = hide ? 0 : actionBarHeight;
        appBarLayout.setLayoutParams(lp);
        appBarLayout.setExpanded(!hide, true);
    
        hide = !hide;
    
    appbar_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.coordinatorlayout.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"
        tools:context=".MainActivity">
    
        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">
    
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />
    
        </com.google.android.material.appbar.AppBarLayout>
    
        <include layout="@layout/content_main" />
    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    谢谢@Caleb Kleveter这是我给Kotlin的代码

        val appBarLayout = activity?.findViewById<AppBarLayout>(R.id.app_bar_layout)
        val lp = appBarLayout?.layoutParams
        lp?.height = 0;
        appBarLayout?.layoutParams = lp
    
    val-appBarLayout=activity?.findViewById(R.id.app\u-bar\u-layout)
    val lp=appBarLayout?.layoutParams
    lp?高度=0;
    appBarLayout?.layoutParams=lp
    
    您是否找到了解决方案。我也收到了同样的问题,这个问题也可以在Chesse detail屏幕中的Chris Bane奶酪广场中复制。从版面中删除两个文本视图,这个问题将复制。@AftabAli检查,看看是否有帮助。这是什么时候可用的?在com.android中似乎不起作用。支持:设计:22.2.1此代码不起作用。向下滚动时,它再次显示appbarlayout Empty这不起作用。在发布之前,您是否在practic上尝试过此操作?我设置的可见性已消失,高度也为0。仅使用高度0是正确的解决方案。非常好的解决方案。我很好奇,在我的例子中,通过将值设置为0,获得lp如何允许您调整高度(隐藏它)。我认为有用的是,通过做一个简单的,
    lp.height=CoordinatorLayout.LayoutParams.WRAP\u CONTENT或布局所需的任何高度(假设为协调器布局)。
    
        val appBarLayout = activity?.findViewById<AppBarLayout>(R.id.app_bar_layout)
        val lp = appBarLayout?.layoutParams
        lp?.height = 0;
        appBarLayout?.layoutParams = lp