0

嗨我有點困惑於BottomNavigationView職位行爲。爲什麼BottomNavigationView的位置取決於ScrollView滾動條?

爲什麼啓用/禁用滾動條影響調整大小的佈局和位置BottomNavigationView

我創建簡單的示例項目,以顯示我的問題......

有了這個代碼:

<?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:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="sandbox.jarda.cz.bottomnavigationtest.MainActivity"> 

    <FrameLayout 
     android:id="@+id/content" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 

     <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scrollbars="none"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical"> 

       <EditText 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:hint="test" /> 

      </LinearLayout> 
     </ScrollView> 

    </FrameLayout> 

    <android.support.design.widget.BottomNavigationView 
     android:id="@+id/navigation" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:background="?android:attr/windowBackground" 
     app:menu="@menu/navigation" /> 
</LinearLayout> 

請問BottomNavigationView住宿鍵盤下(打開時)...

<code>BottomNavigationView</code> is under the keyboard

但是:)

當我刪除線/支持滾動條

<ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

主要佈局 「調整大小」 和BottomNavigationView是在鍵盤的頂部...

<code>BottomNavigationView</code> is on the top of the keyboard

WHY ?

THX的答案:)

回答

1

是不是因爲滾動條的只是你只是需要指定windowSoftInputMode使用android:windowSoftInputMode="adjustPan|adjustResize"在你的活動清單標籤這樣

<activity android:name=".MainActivity" 
     android:windowSoftInputMode="adjustPan|adjustResize"> 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 

     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 
+0

是的,這是工作=> BottomNavigationView是下鍵盤......但我的問題是,爲什麼滾動條影響該定位? :) – Jarda

+0

@Jarda **「adjustResize」**非常重要,如果您的UI包含用戶在執行文本輸入之後或之後可能需要立即訪問的控件。 **例如**,如果您使用相對佈局在屏幕底部放置按鈕欄,則使用「adjustResize」調整佈局大小,以使按鈕欄顯示在輸入法上方。 –

+0

我認爲不是因爲scrollbars @ Jarda –

2

這是不是因爲滾動,這是因爲您的應用程序的鍵盤行爲

你需要在你的manifest activity標記指定windowSoftInputMode更多有關windowSoftInputMode檢查這個link

<application ... > 
<activity 
    android:windowSoftInputMode="adjustResize" ... > 
    ... 
</activity> 
... 

1

的原因是滾動型。當您打開鍵盤時,它會使用可用空間調整自身大小,不包括鍵盤+底部導航視圖的高度。

當鍵盤未顯示它使用可用高度時,不包括導航視圖的高度。


爲了更加清晰,實現鍵盤偵聽器並計算滾動視圖的高度。您可以修改以下代碼

調用代碼。可到onCreate

container.getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener); // Here container is topmost LinearLayout in your layout

而且mOnGlobalLayoutListener

private ViewTreeObserver.OnGlobalLayoutListener mOnGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() { 

    @Override 
    public void onGlobalLayout() { 
     Rect r = new Rect(); 
     mActivityRootView.getWindowVisibleDisplayFrame(r); 
     int screenHeight = mActivityRootView.getRootView().getHeight(); 
     int keyboardheight = screenHeight - r.bottom; 
     /** 
     * 
     * We are taking 15% of the screen height as it should be sufficient space for the keyboard to determine if keyboard is open. 
     */ 
     if (keyboardheight > screenHeight * 0.15) { 
      // Keyboard showing 
      // PRINT HEIGHT OF SCROLLVIEW while keyboard is showing 
     } else { 
      // Keyboard hidden 
      // PRINT HEIGHT OF SCROLLVIEW while keyboard not showing 
     } 
    } 
};