4

我想在我的工具栏下方有一个内容区域。它的行为应该是在滚动时滚动并在滚动时立即进入。工具栏应该保持不动,不要滚动。AppBarLayout滚动内容下方工具栏

我的布局是这样的:

<?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:id="@+id/coordinator_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".ui.MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.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" /> 

     <RelativeLayout 
      android:id="@+id/box_search" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="16dp" 
      android:layout_marginLeft="16dp" 
      android:layout_marginRight="16dp" 
      app:layout_scrollFlags="scroll|enterAlways"> 

      <TextView 
       android:id="@+id/text_search_filter" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Line 1" 
       android:textColor="#FFF" /> 

      <TextView 
       android:id="@+id/text_search_category" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/text_search_filter" 
       android:text="Line 2" 
       android:textColor="#FFF" /> 

      <TextView 
       android:id="@+id/text_search_location" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/text_search_category" 
       android:text="Line 3" 
       android:textColor="#FFF" /> 
     </RelativeLayout> 
    </android.support.design.widget.AppBarLayout> 

    <include layout="@layout/content_main" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@drawable/ic_add_white_24dp" /> 
</android.support.design.widget.CoordinatorLayout> 

如果我把上面的工具栏,我能够得到想要的效果内容区域,但显然这是在错误的位置。

如果我把下面的工具栏没有滚动内容区域...

回答

2

如果我把上面的工具栏,我能够得到想要的效果 内容区域,但显然这是在错误的位置。

当然是在错误的位置,因为:http://www.google.com/design/spec/layout/structure.html#structure-app-bar

应用栏,原名的动作条中的Android是一种特殊 一种工具栏的多数民众赞成用于品牌推广,导航,搜索和 操作。

因此,您需要添加CollapsingToolbarLayout及其上的内容。

和:

我想有我下面的工具栏内容区域。 它的行为应该是 滚动时向上滚动并立即进入,而 向下滚动时,工具栏应该保持在它的位置,没有任何 滚动。

要不要补充说CollapsingToolbarLayout,你可能需要添加app:layout_collapseMode="pin"Toolbar后滚动Toolbar

+0

刚刚尝试过。一切崩溃,包括工具栏(甚至与应用程序:layout_collapseMode =“pin”)... :( – Alessandro

+0

我猜你正在使用错误的标志为'CollapsingToolbarLayout',检查此链接:http://guides.codepath .com/android/Handling-Scrolls-with-CoordinatorLayout但是我要说的是,这是最好的方式! – Mohsen

+0

我正在使用app:layout_scrollFlags =“scroll | enterAlways”。谢谢你的帮助,我知道这是正确的方式做到这一点,但我只是不能... – Alessandro

1

更新:

因为我原来的建议没有工作,达到预期的结果,另一种方法是打出来的工具栏来自CoordinatorLayout。可以按如下方式构建您的布局XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    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:orientation="vertical"> 
    <android.support.v7.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" /> 
    <android.support.design.widget.CoordinatorLayout 
     android:id="@+id/coordinator_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:context=".ui.MainActivity"> 
     <android.support.design.widget.AppBarLayout 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      android:theme="@style/AppTheme.AppBarOverlay"> 
      <RelativeLayout 
       android:id="@+id/box_search" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="16dp" 
       android:layout_marginRight="16dp" 
       android:layout_marginBottom="16dp" 
       app:layout_scrollFlags="scroll|enterAlways"> 
       <TextView 
        android:id="@+id/text_search_filter" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="Line 1" 
        android:textColor="#FFF" /> 
       <TextView 
        android:id="@+id/text_search_category" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_below="@id/text_search_filter" 
        android:text="Line 2" 
        android:textColor="#FFF" /> 
       <TextView 
        android:id="@+id/text_search_location" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_below="@id/text_search_category" 
        android:text="Line 3" 
        android:textColor="#FFF" /> 
      </RelativeLayout> 
     </android.support.design.widget.AppBarLayout> 
     <include layout="@layout/content_main" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 
     <android.support.design.widget.FloatingActionButton 
      android:id="@+id/fab" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom|end" 
      android:layout_margin="@dimen/fab_margin" 
      android:src="@drawable/ic_add_white_24dp" /> 
    </android.support.design.widget.CoordinatorLayout> 
</LinearLayout> 

如果仍然没有工作,你应该考虑不使用包含语句,这可能是问题的根源。相反,将布局定义直接引入此布局,并放置在NestedScrollView(将apply:layout_behavior =“@ string/appbar_scrolling_view_behavior”应用于此NestedScrollView)下。如果您需要其他帮助,请在您的OP中发布content_main布局XML的内容。

原始响应:

确保您定义的应用程序:layout_behavior为你的主要内容,它定义了主要内容的滚动应该影响AppBarLayout。具体来说,试试这个:

<include layout="@layout/content_main" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 
+0

我试过了,但没有运气... – Alessandro

+0

从AppBarLayout中提取工具栏会产生可怕的结果,工具栏将在内容下进行... – Alessandro

+0

发布content_main布局的内容。让我们看看你有什么 –