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

60

已经在这里提问,但是没有得到一个恰当的答案。

我想要FAB浮动在键盘的顶部。仅此而已。

例如:

  1. 使用Android Studio打开一个新的空白活动(Blank Activity)项目模板
  2. 将Hello World TextView更改为EditText
  3. 请参阅下面的图像:

进入图像描述

3个回答

92

原来这很简单,

  • 在manifest文件中的activity里添加android:windowSoftInputMode="adjustResize"
  • 确保你的layout xml中根视图有android:fitsSystemWindows="true"属性

5
对于LinearLayout来说,即使没有设置android:fitsSystemWindows="true",它也能正常工作。 - Daniel Olszewski
5
可以通过编程实现:this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); - drmrbrewer
1
它与以下代码不兼容: this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); - Greelings
对于 ConstraintLayout,它也可以在没有 android:fitsSystemWindows="true" 的情况下正常工作。 - ABN
在协调布局中,即使没有使用android:fitsSystemWindows="true"也可以正常工作。 - Abhishek Saxena

6
如果你想要看到FloatingActionBar在键盘上面移动,你需要: 1) 将FloatingActionBar插入到CoordinatorLayout中:
<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>

2) 将以下代码添加到根视图(FAB所在的位置):

android:fitsSystemWindows="true".

例如:

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

3) 将以下代码添加到AndroidManifest.xml文件中的Activity中:

android:windowSoftInputMode="adjustResize"

例如:

<activity
        android:name=".MainActivity"
        android:windowSoftInputMode="adjustResize"/>

0
如果您正在使用特定的碎片,则可以使用此代码,对于活动请相应地进行更改。
    view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Rect r = new Rect();
            view.getWindowVisibleDisplayFrame(r);
            if (v.getRootView().getHeight() - (r.bottom - r.top) > 500) {
                //Log.d("keyboardStatus","opened");
                fab.setVisibility(View.GONE);
            } else {
               // Log.d("keyboardStatus","closed");
                fab.setVisibility(View.VISIBLE);
            }
        }
    });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接