【问题标题】:Issue to hide Toolbar when scrolling in android在android中滚动时隐藏工具栏的问题
【发布时间】:2015-08-17 10:02:53
【问题描述】:

我有一个带有 NavigationDrawer 和工具栏的 Activity。当我想要在滚动一个子片段视图时消失一个工具栏时,我遇到了一个问题。一切正常,除了从底部出现的一些视图与正在消失的工具栏的大小完全相同。

我制作了一个 GIF 动画来显示问题。由于我的声誉,我无法直接发布图片,但 this gifthis gif 显示了问题

我试图弄清楚这是从哪里来的。似乎它来自我的容器 FrameLayout,我的片段视图在运行时放置在其中。我将其背景更改为绿色,以便我可以识别它。

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

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

    <include
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|enterAlways"
        layout="@layout/toolbar" />

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/darkGreen"
        tools:context="org.cddevlib.breathe.MainActivity" />
</LinearLayout>

<fragment
    android:id="@+id/navigation_drawer"
    android:name="org.cddevlib.breathe.NavigationDrawerFragment"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    tools:layout="@layout/fragment_navigation_drawer" /> </android.support.v4.widget.DrawerLayout>

这是包含的工具栏布局

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/toolbar"
  android:layout_width="match_parent"
  android:layout_height="?attr/actionBarSize"
  app:layout_collapseMode="pin"
  app:layout_scrollFlags="scroll|enterAlways"
  android:fitsSystemWindows="true"
  app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>

最后是加载的片段视图中的 sn-p:

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/black" >

   <include
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_scrollFlags="scroll|enterAlways"
    layout="@layout/toolbar" />

    <android.support.v4.widget.NestedScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:background="@color/white"
        android:fillViewport="true" >

        (...)

请注意,我在该布局中再次包含工具栏,因为我希望通过隐藏/添加工具栏为我的应用程序中的每个片段使用不同的工具栏。

    toolbar = (Toolbar) ((View) vw).findViewById(R.id.toolbar);
            if (toolbar != null) {
                // // for crate home button
                activity = (AppCompatActivity) getActivity();
                activity.getSupportActionBar().hide();
                toolbar.setBackgroundDrawable(new ColorDrawable((ColorUtils.getColorDark(DataModule.getInstance()
                        .getMainActivity()))));
                activity.setSupportActionBar(toolbar);
                activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            }

解决方案

因此,感谢 sanat shukla 的回答,我的问题是我在片段视图中使用了 AppBarLayout 太多。在我的片段视图中,AppBarLayout 是我所有组件的主要布局,但它不应该!它旨在保存工具栏内容!谢谢

【问题讨论】:

    标签: android android-fragments material-design


    【解决方案1】:

    这不是问题。它是android提供的酷炫动画和支持。当您使用带有工具栏的协调器布局时,它会在滚动列表时显示很酷的动画。

    设计库将此提升到一个新的水平:使用 AppBarLayout 允许您的工具栏和其他视图(例如由 TabLayout) 以对标有 a 的兄弟视图中的滚动事件做出反应 滚动视图行为

    从这里阅读: http://android-developers.blogspot.in/2015/05/android-design-support-library.html

    如果您不想隐藏工具栏,请不要使用协调器布局和带有工具栏的 appbar。

    试试这个片段:

    <android.support.design.widget.CoordinatorLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
         <! -- Your Scrollable View -->
        <android.support.v4.widget.NestedScrollView
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/scrollView1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                android:background="@color/white"
                android:fillViewport="true" >
        <android.support.v4.widget.NestedScrollView/>
        <android.support.design.widget.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
        <include
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_scrollFlags="scroll|enterAlways"
                layout="@layout/toolbar" />
         </android.support.design.widget.AppBarLayout>
    </android.support.design.widget.CoordinatorLayout>
    

    【讨论】:

    • 实际上我想要那个动画,但我不想要底部的绿色条,如下所示i.stack.imgur.com/Pe5Tl.gif
    • 它不是酒吧。它是你的框架布局
    • 是的,谢谢,它似乎是我的 FrameLayout 显示在底部。但为什么?我将 NestedScrollViews 布局更改为您的,但没有效果
    • 我是否为我的片段使用了正确的布局 (FrameLayout) 作为容器?
    • @Christian Dud 您添加了两个 AppBar 布局,一个在您的工具栏文件中,一个在您的片段中。在您的工具栏文件中,只需添加工具栏并从那里删除协调器布局。然后使用 AppBar 布局将 Coordinator 布局添加到您的片段中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多