【问题标题】:How to place views inside Coordinatorlayout如何在 Coordinatorlayout 中放置视图
【发布时间】:2020-11-03 16:23:43
【问题描述】:

我需要使用Coordinatorlayout 才能让layout_behaviour attr 在滚动时隐藏FAB

通常我只会将我的 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>

【问题讨论】:

  • 你试过android:layout_gravity="end|bottom"

标签: android android-layout kotlin


【解决方案1】:

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>

【讨论】:

  • 其实使用巨额利润是最让我害怕的,没有办法解决吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-28
  • 1970-01-01
  • 2018-05-06
  • 1970-01-01
  • 1970-01-01
  • 2016-07-02
  • 2021-09-17
相关资源
最近更新 更多