'app:layout_behavior' 应该在哪里设置?

新手上路,请多包涵

它应该设置在 AppBarLayout 同级的父级还是在其同级内的第一个 Scrollable View 中?


使用 Material Design for Android ,有一些 视图 可以让我们根据其周围环境来处理布局的行为,其中之一是 CoordinatorLayout ,正如 本 CodePath 指南 所提到的:

CoordinatorLayout 扩展了完成许多 Google 的 Material Design 滚动效果的能力。目前,此框架中提供了几种方法,无需编写您自己的自定义动画代码即可使其工作。

我现在感兴趣的是:

  • 扩展或收缩工具栏或标题空间以为主要内容腾出空间。

因此,我们将使用带有 app:layout_scrollFlags 设置的 工具栏AppBarLayout 和带有 app:layout_behaviorAppBarLayout 的另一个 ViewGroup 兄弟。

我的问题是:我们应该将 app:layout_behavior 放在哪个确切的 ViewGroup(或者可能是 View)中?


到目前为止,我已经尝试过(他们都 工作过,他们都是 AppBarLayout 的兄弟姐妹):

  • 滚动视图

  • 可滚动视图中的第一个 ViewGroup

  • ViewGroup 中的 ScrollView

而这个没有用:

  • 没有 Scrollable View 子项的 ViewGroup。

网上有很多例子,但没有一个真正说明你应该把它放在哪里,比如:

http://www.ingloriousmind.com/blog/quick-look-on-the-coordinatorlayout/ https://guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout https://developer.android.com /training/basics/firstapp/building-ui.html https://www.bignerdranch.com/blog/becoming-material-with-android-design-support-library/

原文由 Evin1_ 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2.1k
2 个回答

检查此链接: https ://developer.android.com/reference/android/support/design/widget/AppBarLayout.html

AppBarLayout 还需要一个单独的滚动兄弟,以便知道何时滚动。绑定是通过 AppBarLayout.ScrollingViewBehavior 类完成的, 这意味着您应该将滚动视图的行为设置为 AppBarLayout.ScrollingViewBehavior 的实例。包含完整类名的字符串资源可用。

他们提到过,应该是 View 将显示在 AppBarLayout 下,如下所示:

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

     <android.support.v4.widget.NestedScrollView
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             app:layout_behavior="@string/appbar_scrolling_view_behavior">

         <!-- Your scrolling content -->

     </android.support.v4.widget.NestedScrollView>

     <android.support.design.widget.AppBarLayout
             android:layout_height="wrap_content"
             android:layout_width="match_parent">

         <android.support.v7.widget.Toolbar
                 ...
                 app:layout_scrollFlags="scroll|enterAlways"/>

         <android.support.design.widget.TabLayout
                 ...
                 app:layout_scrollFlags="scroll|enterAlways"/>

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

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


我的问题是: 我们应该把 app:layout_behavior 确切地说成什么 ViewGroup (或者可能是 View )?

在这个链接中:http: //guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout

接下来,我们需要定义 AppBarLayout将被滚动的 View 之间的关联。将 app:layout_behavior 添加到 RecyclerView 或任何其他能够嵌套滚动的视图,例如 NestedScrollView 。支持库包含一个特殊的字符串资源 @string/appbar_scrolling_view_behavior 映射到 AppBarLayout.ScrollingViewBehavior ,用于在此特定视图上发生滚动事件时通知 AppBarLayout 。必须在触发事件的视图上建立行为。

原文由 ʍѳђઽ૯ท 发布,翻译遵循 CC BY-SA 3.0 许可协议

AppBarLayout 还需要一个单独的滚动兄弟,以便知道何时滚动。

Android的这个描述非常不完整,导致我浪费了数小时的时间。

滚动兄弟 是用词不当,不必是任何类型的滚动视图。

For example, below my AppBarLayout , I’m using a ViewPager2 that will render a Fragment that will render a Scrollview , so I需要在主布局中直接在 ViewPager2 上设置 app:layout_behavior="@string/appbar_scrolling_view_behavior" ,而 不是 在片段布局中深度嵌套的 Scrollview

我也没有用滚动 AppBarLayout 或其任何子项在屏幕上或屏幕外,所以我错误地认为我可以不设置 app:layout_behavior 任何地方。

错误的。

这揭示了一个更隐蔽的问题: AppBarLayout 需要 _滚动兄弟_,是的。但 不仅仅是“知道何时滚动”,而是实际调整兄弟姐妹的大小以使其正确地适应屏幕!否则,兄弟姐妹将保持其配置的大小,并将被向下轻移到屏幕外 AppBarLayout 的高度!您甚至可以在 Android Studio 的布局编辑器中看到这一点。

长话短说:如果您要使用 AppBarLayout ,您 需要app:layout_behavior="@string/appbar_scrolling_view_behavior" 标记您的一个视图,无论它是否是滚动视图。

原文由 rmirabelle 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题