android - 如何使用 insetEdge 和 dodgeInsetEdges 避免 View 重叠

标签 android android-coordinatorlayout

我有这样的布局:

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/listitem_1"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_margin="16dp"
                android:gravity="center"
                android:text="item 1"
                android:background="#ff0000"/>

            <TextView
                android:id="@+id/listitem_2"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_margin="16dp"
                android:gravity="center"
                android:text="item 2"
                android:background="#ff0000"/>

            <TextView
                android:id="@+id/listitem_3"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_margin="16dp"
                android:gravity="center"
                android:text="item 3"
                android:background="#ff0000"/>

            <View
                android:id="@+id/scroller_bottom_bar"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:background="#0000ff" />
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        app:layout_anchor="@id/scroller_bottom_bar"
        app:layout_anchorGravity="end"
        app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>

转换为以下 UI 动画:

enter image description here

如您所见, float 操作按钮与蓝色底部 View 重叠。我想避免这种情况。我该怎么做?

最佳答案

可以通过添加 insetEdgedodgeInsetEdges 属性来修复此问题。基本上,insetEdgeCoordinatorLayout 知道它的其他子级可以根据需要躲避它。在其他 View 上设置 dodgeInsetEdges 让它们真正躲避可躲避的 View (由 insetEdge 标记)。

将原来的xml布局重写为:

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/listitem_1"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_margin="16dp"
                android:gravity="center"
                android:text="item 1"
                android:background="#ff0000"/>

            <TextView
                android:id="@+id/listitem_2"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_margin="16dp"
                android:gravity="center"
                android:text="item 2"
                android:background="#ff0000"/>

            <TextView
                android:id="@+id/listitem_3"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_margin="16dp"
                android:gravity="center"
                android:text="item 3"
                android:background="#ff0000"/>

            <View
                android:id="@+id/scroller_bottom_bar"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:background="#0000ff" />
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        app:layout_anchor="@id/fab_anchor"
        app:layout_anchorGravity="end"
        app:layout_dodgeInsetEdges="bottom"
        app:srcCompat="@android:drawable/ic_dialog_email" />

    <View
        android:id="@+id/fab_anchor"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        app:layout_anchor="@id/scroller_bottom_bar"
        app:layout_insetEdge="bottom" />
</android.support.design.widget.CoordinatorLayout>

应该产生预期的 UI 行为:

enter image description here

关于android - 如何使用 insetEdge 和 dodgeInsetEdges 避免 View 重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57737838/

相关文章:

android - 在RecyclerView中添加页眉和页脚

android - 在 TextInputLayout 中输入一些文本时 Espresso 出错

android - 为什么 GCM 需要 google 帐户登录才能在 4.0.4 操作系统以下的设备上工作?

android - 如何让 snackbar 和工厂一起搬家

android - CoordinatorLayout 带有折叠工具栏,NestedScrollView 和 RecyclerView 底部有空白空间(滚动太远)

android - 扩展持久性 Bottom Sheet 时调整回收器 View 高度

android - 总安装量与活跃安装量的混淆。 38 个总安装量突然增加到 92 个,但不是活跃安装

android - 删除项目时 Recyclerview 清除查看器

android - RecyclerView 位于工具栏下方

android - AppBar 在滚动时隐藏 View 直到折叠