簡體   English   中英

出現軟鍵盤時如何避免只有BottomNavbar向上推

[英]How to avoid only BottomNavbar to pushed up when soft keyboard appears

我的 android 應用程序中有這樣的視圖

在此處輸入圖像描述

有一個mainActivity有那個bottomNavBar並且有一個片段內部有底部有兩個按鈕的活動。

我應該做的是,當軟鍵盤出現時, bottomNavBar應該保持在底部(鍵盤后面)但是這兩個按鈕應該被向上推並在鍵盤頂部可見

如果我在manifest文件中為 mainActivity 設置android:windowSoftInputMode="adjustResize"然后鍵盤向上推bottomNavBar和按鈕。

如果我將android:windowSoftInputMode="adjustResize"設置為片段(以編程方式),那么它仍然顯示相同的行為。

我怎樣才能做到這一點? 任何建議將不勝感激

這是我的活動 xml

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

我也面臨同樣的問題,經過多次努力,我找到了一個適合我的解決方案。我做什么,當鍵盤應用程序使bottomNavBar可見性消失時,反之亦然

試試這個讓鍵盤打開關閉事件 -

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

感謝更新。 我試過你的布局。 在適當的活動上添加清單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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM