在键盘上方设置FAB(浮动操作按钮)

Dav*_*vid 44 android android-softkeyboard floating-action-button

已经在这里过,但没有正确答案.

我希望FAB浮在键盘上.而已.

例如

  1. Blank Activity使用Android Studio 打开一个新模板项目
  2. 将Hello World更改TextViewEditText
  3. 见下图:

在此输入图像描述

Dav*_*vid 70

事实证明这很简单,

  • 添加android:windowSoftInputMode="adjustResize"到清单中的活动
  • 确保布局xml中的根视图具有android:fitsSystemWindows="true"属性

  • 或者,以编程方式:`this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);` (4认同)
  • 它没有`android:fitsSystemWindows ="true"`,至少对于'LinearLayout`. (2认同)

小智 6

如果你想看到 FloatingActionBar 移动到键盘上方,你应该:

1) 在CoordinatorLayout中插入FloatingActionBar:

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="16dp"
        android:src="@drawable/ic_img" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

2)在根视图(FAB所在的位置)中添加代码:

android:fitsSystemWindows="true".
Run Code Online (Sandbox Code Playgroud)

例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".AddFilm">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="16dp"
            android:src="@drawable/ic_img" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

3) 将 AndroidManifest.xml 中的下一个代码添加到您的 Activity 中:

android:windowSoftInputMode="adjustResize"
Run Code Online (Sandbox Code Playgroud)

例如:

<activity
        android:name=".MainActivity"
        android:windowSoftInputMode="adjustResize"/>
Run Code Online (Sandbox Code Playgroud)