首页 > 解决方案 > 如何在 Coordinatorlayout 中放置视图

问题描述

我需要使用Coordinatorlayout才能让layout_behaviourattrFAB在滚动时隐藏。

通常我只会将我的 FAB 放在底端,但这次我实现了可扩展的 fab,我发现很难在 Coordinatorlayout 中将 4 个 FABS 放在彼此之上。

有什么想法我能做什么?我不介意以任何一种方式获得解决方案(在没有 Coordinatorlayout 的情况下处理隐藏在 API 23 下方的 webview 滚动上的晶圆厂,或者将晶圆厂正确地放在彼此之上Coordinatorlayout

目前这是我的ConstraintLayout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
    

    <WebView
        android:id="@+id/webView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </WebView>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:focusable="true" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:clickable="true"
        android:focusable="true"
        app:layout_constraintBottom_toTopOf="@+id/fab1"
        app:layout_constraintEnd_toEndOf="@+id/fab1"
        app:layout_constraintStart_toStartOf="@+id/fab1" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:clickable="true"
        android:focusable="true"
        app:layout_constraintBottom_toTopOf="@+id/fab2"
        app:layout_constraintEnd_toEndOf="@+id/fab2"
        app:layout_constraintStart_toStartOf="@+id/fab2" />
    

</androidx.constraintlayout.widget.ConstraintLayout>

标签: androidandroid-layoutkotlin

解决方案


FAB 必须是协调器布局的直接子级

您可以使用

 app:layout_anchor="@id/****"
 app:layout_anchorGravity="center" 

这个属性

对于您的情况,您可以更改 xml,如:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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">


<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</WebView>

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="16dp"
    android:layout_gravity="bottom|end"
    android:clickable="true"
    android:focusable="true" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="100dp"
    android:layout_marginEnd="0dp"
    app:layout_anchor="@id/fab1"
    app:layout_anchorGravity="center"
    android:clickable="true"
    android:focusable="true" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginBottom="200dp"
        android:clickable="true"
        android:focusable="true"
        app:layout_anchor="@id/fab2"
        app:layout_anchorGravity="center"/>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

推荐阅读