已经在这里提问,但是没有得到一个恰当的答案。
我想要FAB浮动在键盘的顶部。仅此而已。
例如:
- 使用Android Studio打开一个新的
空白活动(Blank Activity)
项目模板 - 将Hello World
TextView
更改为EditText
- 请参阅下面的图像:
已经在这里提问,但是没有得到一个恰当的答案。
我想要FAB浮动在键盘的顶部。仅此而已。
例如:
空白活动(Blank Activity)
项目模板TextView
更改为EditText
原来这很简单,
android:windowSoftInputMode="adjustResize"
android:fitsSystemWindows="true"
属性<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"/>
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);
}
}
});
LinearLayout
来说,即使没有设置android:fitsSystemWindows="true"
,它也能正常工作。 - Daniel Olszewskithis.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
。 - drmrbrewerandroid:fitsSystemWindows="true"
也可以正常工作。 - Abhishek Saxena