簡體   English   中英

'app:layout_behavior' 應該在哪里設置?

[英]Where should 'app:layout_behavior' be set?

它應該設置在AppBarLayout兄弟姐妹的父級還是其兄弟姐妹內的第一個可滾動視圖?


使用Material Design for Android ,有一些 視圖可以讓我們根據其周圍環境處理布局的行為,其中之一是CoordinatorLayout ,正如本 CodePath 指南所述

CoordinatorLayout 擴展了完成許多 Google Material Design 滾動效果的能力。 目前,該框架提供了多種方式,使其無需編寫您自己的自定義動畫代碼即可工作。

我現在感興趣的是:

  • 擴展或收縮工具欄或標題空間,為主要內容騰出空間。

因此,我們將使用AppBarLayout一個工具欄app:layout_scrollFlags設置和其他的ViewGroup兄弟到AppBarLayoutapp:layout_behavior

我的問題是:我們應該將該app:layout_behavior放在哪個 ViewGroup(或者可能是 View)中?


到目前為止,我已經嘗試過(並且它們都有效,並且它們都是 AppBarLayout 的兄弟):

  • 滾動視圖

  • 可滾動視圖中的第一個 ViewGroup

  • ViewGroup 內的 ScrollView

而這個沒有用:

  • 沒有可滾動視圖子項的 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/

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

AppBarLayout還需要一個單獨的滾動兄弟,以便知道何時滾動。 綁定是通過AppBarLayout.ScrollingViewBehavior類完成的,這意味着您應該將滾動視圖的行為設置為AppBarLayout.ScrollingViewBehavior的實例 包含完整類名的字符串資源可用。

他們提到了這一點,它應該是將在AppBarLayout下顯示的View ,如下所示:

<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 支持庫包含映射到AppBarLayout.ScrollingViewBehavior的特殊字符串資源@string/appbar_scrolling_view_behavior ,用於在此特定視圖上發生滾動事件時通知AppBarLayout 行為必須建立在觸發事件的視圖上。

請確保你在String.xml添加appbar_scrolling_view_behavior

<!-- The class name to the ScrollingChildBehavior required for AppBarLayout -->
<string name="appbar_scrolling_view_behavior" translatable="false">android.support.design.widget.AppBarLayout$ScrollingViewBehavior</string>

眾所周知,我們可以像下面這樣使用它

<android.support.v7.widget.RecyclerView
        android:id="@+id/rvSomeList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

它只是為了信息而不是 OP 答案。

app:layout_behavior應設置為協調器布局的直接子視圖

我必須將以下內容添加到 gradle 文件中,否則它會給我一個編譯錯誤。

implementation 'com.google.android.material:material:1.0.0'

希望這也能幫助其他人!

對於將CoordinatorLayoutFragmentContainerAppBarLayout一起使用的人:

  • 在容器上設置app:layout_behavior真的很好(不僅僅是在NestedScrollViewRecyclerView )。 它刪除FragmentContainer不必要的底部邊距,並保證在顯示鍵盤時隱藏應用欄。

AppBarLayout 還需要一個單獨的滾動兄弟,以便知道何時滾動。

Android 的這個描述非常不完整,導致我浪費了數小時的時間。

滾動同級是用詞不當,不需要是任何類型的滾動視圖。

例如,在我的AppBarLayout下方,我使用的是ViewPager2 ,它將呈現一個將呈現ScrollviewFragment ,所以我需要直接在主布局的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"標記您的視圖之一,無論它是否是滾動視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM