【问题标题】:How to use Floating Button Over Vertical Scroll View?如何在垂直滚动视图上使用浮动按钮?
【发布时间】:2019-11-18 22:43:12
【问题描述】:

Gmail Android 应用浮动按钮

我想在 Android 的滚动视图上添加一个浮动按钮。这样,即使我滚动布局,在这种情况下,按钮也应该在给定位置(右下角)保持不变。就像 Gmail android 移动应用一样,它们在右下角有一个完整的发送邮件按钮,并且背景是可滚动的。

【问题讨论】:

标签: java android floating-action-button


【解决方案1】:

如果您将按钮放在布局顶部而不是在回收站视图中,则滚动时它不会移动。

<?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"
android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="match_parent">



<androidx.recyclerview.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/floatingActionButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:clickable="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:srcCompat="@android:drawable/ic_input_add" />
</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

    【解决方案2】:

    这不是ScrollView,而是RecyclerView。为此,您可以使用CoordinatorLayout

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.coordinatorlayout.widget.CoordinatorLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <androidx.appcompat.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"/>
        </com.google.android.material.appbar.AppBarLayout>
    
        <Your layout with Recycler View/>
    
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:src="@android:drawable/ic_menu_edit" />
    
    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    如果您仍想使用 ScrollView,只需将其作为具有重力底部的 RecyclerView 的兄弟。

    【讨论】:

      【解决方案3】:

      在约束布局内制作滚动视图不要使主父布局成为滚动视图。

      <?xml version="1.0" encoding="utf-8"?>
      <android.support.constraint.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">
      
          <android.support.design.widget.FloatingActionButton
              android:id="@+id/floatingActionButton"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginEnd="8dp"
              android:layout_marginRight="8dp"
              android:layout_marginBottom="8dp"
              android:clickable="true"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              tools:srcCompat="@tools:sample/avatars[0]" />
      
          <ScrollView
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent">
      
              <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical" />
          </ScrollView>
      </android.support.constraint.ConstraintLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-30
        • 1970-01-01
        • 1970-01-01
        • 2013-02-10
        • 1970-01-01
        相关资源
        最近更新 更多