2017-02-05 51 views
14

根據material design spec,當鍵盤出現時,BottomNavigationView應該隱藏在它下面。但是,如果我在活動清單中設置android:windowSoftInputMode="adjustResize",則BottomNavigationView會在鍵盤上方移動。如何隱藏BottomNavigationView下方的鍵盤adjustResize集

我需要設置adjustResize才能在鍵盤打開時滾動到屏幕底部。但是,我不希望BottomNavigationView可見。這可以做到嗎?

如何它目前看起來:

enter image description here

佈局XML(在現實中會有一個FrameLayout其中EditText是和EditText將裏面):

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="Input" 
     android:layout_gravity="center" 
     android:layout_centerVertical="true"/> 

    <android.support.design.widget.BottomNavigationView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     app:itemBackground="@color/colorPrimary" 
     app:menu="@menu/menu_bottom_navigation" 
     app:itemIconTint="@android:color/white" 
     app:itemTextColor="@android:color/white"/> 

</RelativeLayout> 
+0

你解決了嗎? –

+0

不,我不知道,但有一個建議是,當一個字段被點擊時(包括底部導航欄),然後當用戶點擊屏幕上方的屏幕或滾動鍵盤時,隱藏鍵盤背後的所有東西就會消失。這不是很好,但我認爲這比浮動導航欄更好的用戶體驗。 Spotify應用程序執行此操作。 – willjgriff

+0

我有同樣的問題...如果你找到一個解決方案,請告訴我... –

回答

-1

由於一個替代方式與您android:windowSoftInputMode="adjustResize"你可以試試這個。

從您的OnCreate調用此方法 - 鍵盤啓動後,您可以更改不需要顯示的視圖的可見性! 鍵盤關閉時再次顯示它們。

public void checkKeyBoardUp(){ 
     rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
      @Override 
      public void onGlobalLayout() { 
       Rect r = new Rect(); 
       rootView.getWindowVisibleDisplayFrame(r); 
       int heightDiff = rootView.getRootView().getHeight() - (r.bottom - r.top); 

       if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard... 
        //ok now we know the keyboard is up... 
        whatEverView.setVisibility(View.INVISIBLE); 

       }else{ 
        //ok now we know the keyboard is down... 
        whatEverView.setVisibility(View.VISIBLE); 
       } 
      } 
     }); 
    } 
+0

其正確的解決方案,但爲了正確檢查鍵盤狀態,使用此[鏈接](http://stackoverflow.com/a/ 4737265/920379) –

+2

這不起作用:空白空間停留在導航欄的位置 –

16

這個清單中的

android:windowSoftInputMode="adjustPan" 

添加到您的活動所以像

<activity android:name=".feature.home.HomeActivity" 
android:windowSoftInputMode="adjustPan"/> 
0

還有另一種解決方案,它不需要adjustSpan,但它僅適用於API >= 21。您可以通過跟蹤系統插件檢測鍵盤是否顯示/隱藏。假設你有BottomNavigationView,這是LinearLayout孩子,你需要隱藏它時,會顯示鍵盤:

> LinearLayout 
    > ContentView 
    > BottomNavigationView 

所有你需要做的是在這樣的方式擴展LinearLayout

public class KeyboardAwareLinearLayout extends LinearLayout { 
    public KeyboardAwareLinearLayout(Context context) { 
     super(context); 
    } 

    public KeyboardAwareLinearLayout(Context context, @Nullable AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public KeyboardAwareLinearLayout(Context context, 
            @Nullable AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    public KeyboardAwareLinearLayout(Context context, AttributeSet attrs, 
            int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
    } 

    @Override 
    public WindowInsets onApplyWindowInsets(WindowInsets insets) { 
     int childCount = getChildCount(); 
     for (int index = 0; index < childCount; index++) { 
      View view = getChildAt(index); 
      if (view instanceof BottomNavigationView) { 
       int bottom = insets.getSystemWindowInsetBottom(); 
       if (bottom >= ViewUtils.dpToPx(200)) { 
        view.setVisibility(GONE); 
       } else { 
        view.setVisibility(VISIBLE); 
       } 
      } 
     } 
     return insets; 
    } 
} 

的想法是當顯示鍵盤時,系統插入值被改變,具有相當大的值.bottom值。