有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在垂直滚动视图上使用浮动按钮?

Gmail Android应用程序浮动按钮

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

enter image description here


共 (3) 个答案

  1. # 1 楼答案

    将滚动视图设置为约束布局内部不要将主父布局设置为滚动视图

    <?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>
    
  2. # 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与gravity bottom的同级

  3. # 3 楼答案

    如果将按钮放在版面顶部而不是“回收”视图中,则滚动时按钮不会移动

    <?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>