【发布时间】:2016-03-15 10:17:39
【问题描述】:
已经问过here,但没有正确答案。
我希望 FAB 浮动在键盘上方。就是这样。
例如
- 使用 Android Studio 打开一个新的
Blank Activity模板项目 - 将 Hello World
TextView更改为EditText - 见下图:
【问题讨论】:
标签: android android-softkeyboard floating-action-button
已经问过here,但没有正确答案。
我希望 FAB 浮动在键盘上方。就是这样。
例如
Blank Activity 模板项目TextView 更改为 EditText
【问题讨论】:
标签: android android-softkeyboard floating-action-button
如果您想看到 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"/>
【讨论】:
如果您正在使用特定的片段,您可以使用此代码并对活动进行相应的更改。
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);
}
}
});
【讨论】:
事实证明这很容易,
android:windowSoftInputMode="adjustResize" 添加到清单中的活动android:fitsSystemWindows="true" 属性【讨论】:
android:fitsSystemWindows="true" 的情况下工作,至少对于LinearLayout。
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
android:fitsSystemWindows="true" 的情况下工作