如何避免出现软键盘时仅将 BottomNavbar 向上推

How to avoid only BottomNavbar to pushed up when soft keyboard appears

我的 android 应用程序中有这样的视图

有一个 mainActivity 里面有那个 bottomNavBar 里面有片段 activity 底部有两个按钮。

我应该做的是当软键盘出现时 bottomNavBar 应该留在底部(在键盘后面)但是这两个按钮应该向上推并在键盘顶部可见

如果我在 manifest 文件中为 mainActivity 设置 android:windowSoftInputMode="adjustResize",那么键盘也会向上推 bottomNavBar 和按钮。

如果我仅将 android:windowSoftInputMode="adjustResize" 设置为片段(以编程方式),那么它仍然显示相同的行为。

我该怎么做?任何建议将不胜感激

这是我的Activityxml

    <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <LinearLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/mobile_navigation"
            android:layout_weight="@integer/int_two"/>

        <com.google.android.material.bottomnavigation.BottomNavigationView
            app:itemIconTint="@drawable/bottom_navigation_selector"
            app:itemTextColor="@drawable/bottom_navigation_selector"
            android:id="@+id/nav_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="@integer/int_zero"
            android:background="?android:attr/windowBackground"
            app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
            app:menu="@menu/bottom_nav_menu" />

    </LinearLayout>
</layout>

我也面临同样的问题,经过多次努力我找到了一个适用于 me.What 的解决方案,当 keyboard appers make bottomNavBar 可见性 GONE反之亦然.

试试这个获取键盘 openclosed 事件 -

view.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
      Rect r = new Rect();
      view.getWindowVisibleDisplayFrame(r);
      if (view.getRootView().getHeight() - (r.bottom - r.top) > 500) {
        // on Keyboard Open
        llBottomNavBarLayout.setVisibility(View.GONE);
      } else {
        // on keyboard close
        llBottomNavBarLayout.setVisibility(View.VISIBLE);
      }
    });

为了获取视图,我使用

LayoutInflater layoutInflater = getLayoutInflater();
View view = layoutInflater.inflate(R.layout.activity_abc, null);

感谢更新。我试过你的布局。添加到您的清单上适当 activity android:windowSoftInputMode="adjustNothing" 请注意,这也会影响 FAB 和其他锚点视图

<activity
   android:name=".activities.MainActivity"
   android:windowSoftInputMode="adjustNothing">
     <intent-filter>
       <action android:name="android.intent.action.MAIN" />
       <category android:name="android.intent.category.LAUNCHER" />
     </intent-filter>
 </activity>