在 CoordinatorLayout 中的工具栏下方添加视图

2023-11-30

我有以下布局:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    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.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

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

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</android.support.design.widget.CoordinatorLayout>

I add Fragment进入FrameLayout,替换它们。我的一个Fragments 是一个列表,其布局如下:

<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

我的问题是工具栏绘制在列表上方。我试图通过包装内容来解决这个问题CoordinatorLayout into a LinearLayout,这解决了透支问题,但这样应用程序栏滚动行为就不再起作用。

任何帮助深表感谢!


取属性

app:layout_behavior="@string/appbar_scrolling_view_behavior"

off the RecyclerView并把它放在FrameLayout你试图在下面显示Toolbar.

我发现滚动视图行为所做的一件重要的事情是将组件布局在工具栏下方。因为FrameLayout有一个会滚动的后代(RecyclerView), the CoordinatorLayout将获得用于移动的滚动事件Toolbar.


另一件需要注意的事情是:布局行为会导致FrameLayout高度要确定尺寸就好像Toolbar已经滚动了,并与Toolbar完全显示整个视图被简单地向下推,使得视图的底部低于视图的底部CoordinatorLayout.

这让我很惊讶。我期望随着工具栏上下滚动,视图会动态调整大小。因此,如果您的视图底部有一个带有固定组件的滚动组件,则在完全滚动该组件之前,您将看不到该底部组件。Toolbar.

因此,当我想在 UI 底部锚定一个按钮时,我通过将该按钮放在 UI 底部来解决这个问题。CoordinatorLayout (android:layout_gravity="bottom")并向工具栏下方的视图添加等于按钮高度的底部边距。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 CoordinatorLayout 中的工具栏下方添加视图 的相关文章

随机推荐