【问题标题】:Set FAB (Floating Action Button) above keyboard在键盘上方设置 FAB(浮动操作按钮)
【发布时间】:2016-03-15 10:17:39
【问题描述】:

已经问过here,但没有正确答案。

我希望 FAB 浮动在键盘上方。就是这样。

例如

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

【问题讨论】:

    标签: android android-softkeyboard floating-action-button


    【解决方案1】:

    如果您想看到 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>
    

    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"/>
    

    【讨论】:

      【解决方案2】:

      如果您正在使用特定的片段,您可以使用此代码并对活动进行相应的更改。

          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);
                  }
              }
          });
      

      【讨论】:

        【解决方案3】:

        事实证明这很容易,

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

        【讨论】:

        • 它可以在没有android:fitsSystemWindows="true" 的情况下工作,至少对于LinearLayout
        • 或者,以编程方式:this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        • 它不能结合使用:this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        • 它在没有 android:fitsSystemWindows="true" 的情况下也适用于 ConstraintLayout。
        • 在协调器布局中也可以在没有 android:fitsSystemWindows="true" 的情况下工作
        猜你喜欢
        • 2023-04-07
        • 1970-01-01
        • 2023-03-15
        • 2016-08-26
        • 2017-03-09
        • 1970-01-01
        • 2016-07-02
        • 2016-07-26
        相关资源
        最近更新 更多