CoordinatorLayout layout_anchor does not work

无人久伴 提交于 2019-11-30 22:34:56

问题


I just tried out the new Support Design Library with CoordinatorLayout, but I'm having problems anchoring child views. My layout currently looks like this:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinator_layout"
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.Dark.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:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_scrollFlags="enterAlways" />

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

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_anchor="@id/toolbar"
    app:layout_anchorGravity="bottom"/>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    android:src="@drawable/ic_fab_add"
    android:onClick="onAddPlayerClick"
    android:layout_gravity="bottom|end"
    app:elevation="4dp"
    app:borderWidth="0dp"/>

When the RecyclerView list is loaded, the first item is being overlapped by the action bar. I assume the app:layout_anchor is not working properly. I already tried anchoring it to appbar to no success.

Does anyone know what I am doing wrong here?


回答1:


Try this code. You need to use app:layout_behavior in your RecyclerView

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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    android:src="@drawable/ic_fab_add"
    android:onClick="onAddPlayerClick"
    app:elevation="4dp"
    app:borderWidth="0dp"
    app:layout_anchor="@id/appbar"
    app:layout_anchorGravity="bottom|right|end"
    android:clickable="true"/>


来源:https://stackoverflow.com/questions/30675534/coordinatorlayout-layout-anchor-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!