2017-01-02 94 views
14

我正在使用Android應用程序並從設計庫中實現BottomNavigationView。我看過很多例子,我無法弄清楚我的佈局有什麼問題。 BottomNavigationView不顯示爲全寬度。BottomNavigationView不是全寬

另一個問題是狀態欄顏色沒有得到應用。

enter image description here

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
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"> 

<!-- Include the toolbar --> 
<include layout="@layout/toolbar"/> 

<RelativeLayout android:id="@+id/fragment_container" 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 


<android.support.design.widget.BottomNavigationView 
    android:id="@+id/bottom_navigation" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_gravity="bottom" 
    app:itemBackground="@color/colorPrimary" 
    app:itemIconTint="@android:color/white" 
    app:itemTextColor="@android:color/white" 
    app:menu="@menu/bottom_navigation_main"/> 

</android.support.design.widget.CoordinatorLayout> 

toolbar.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.AppBarLayout 
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="wrap_content" 
android:theme="@style/AppTheme.AppBarOverlay"> 

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    app:popupTheme="@style/AppTheme.PopupOverlay"/> 

</android.support.design.widget.AppBarLayout> 

編輯: 的狀態加入溶液顏色不設置

android:fitsSystemWindows="true"

(colorPrimaryDark) status bar color not working on android v21?

回答

39

的BottomNavigationView不顯示爲全寬。

它不應該。

根據design guidelines動作的寬度可以在80dp和168dp之間變化。您定義的兩個操作不足以在整個區域水平填充。
(作爲一個方面說明,也可以根據準則的觀點應該三年和五年行動之間包含的內容。)

如果你想填補BottomNavigationView後面的空間,你可以設置視圖的背景顏色是相同的顏色項目的背景:

<android.support.design.widget.BottomNavigationView 
    android:background="@color/bottom_view_color" 
    app:itemBackground="@color/bottom_view_color" 

    // .... /> 
+0

Bottom Nav在Google Play Newstand中看起來很寬,所以我認爲我的佈局存在問題。我打算很快添加第三個菜單項。 – midhunhk

+0

@midhunhk是的,這是可能的。我爲此添加了解決方案。但安迪是正確的,你應該遵循設計規範。 –

5

我建議只使用

android:background="@color/bottom_view_color" 

它將顯示欄爲全寬,當用戶點擊我會保持連鎖反應電信設備製造商。

如果您還添加app:itemBackground您將失去連鎖效應。

在你的情況

<android.support.design.widget.BottomNavigationView 
    android:id="@+id/bottom_navigation" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_gravity="bottom" 
    android:background="@color/colorPrimary" 
    app:itemIconTint="@android:color/white" 
    app:itemTextColor="@android:color/white" 
    app:menu="@menu/bottom_navigation_main"/> 
5

這是可行的。但它會再次default design specs,我會建議你去default design specs


現在來我的解決方案...

定義下面的尺寸爲dimens.xml。這些尺寸應該在values,values-large,values-large-land600dp可以增加到1000dp或更多在values-large,values-large-land,如果在平板電腦中你沒有看到這種變化。

<resources xmlns:tools="http://schemas.android.com/tools"> 
    <dimen name="design_bottom_navigation_item_max_width" tools:override="true">600dp</dimen> 
    <dimen name="design_bottom_navigation_active_item_max_width" tools:override="true">600dp</dimen> 
</resources> 

這就是它!結果將會像enter image description here


這不是一個馬齊奇。

爲什麼尺寸已經添加具有這樣的名稱和值是正由BottomNavigationMenuView.java爲600dp

兩種尺寸(其正在使用的類來表示在BottomNavigationView菜單)。 下面是代碼

public BottomNavigationMenuView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    ... 
    mInactiveItemMaxWidth = res.getDimensionPixelSize(
      R.dimen.design_bottom_navigation_item_max_width); 
    .... 
    mActiveItemMaxWidth = res.getDimensionPixelSize(
      R.dimen.design_bottom_navigation_active_item_max_width); 
    ..... 
} 

現在,這些值被用來創建視圖固定寬度,如下面

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    .... 
    if (mShiftingMode) { 
     final int inactiveCount = count - 1; 
     final int activeMaxAvailable = width - inactiveCount * mInactiveItemMinWidth; 
     final int activeWidth = Math.min(activeMaxAvailable, mActiveItemMaxWidth); 
     final int inactiveMaxAvailable = (width - activeWidth)/inactiveCount; 
     final int inactiveWidth = Math.min(inactiveMaxAvailable, mInactiveItemMaxWidth); 
     ... 
    } else { 
     final int maxAvailable = width/count; 
     final int childWidth = Math.min(maxAvailable, mActiveItemMaxWidth); 
     ..... 
    } 
    .... 
} 

要始終使用的activeMaxAvailable值,我的虛設值設置爲mActiveItemMaxWidth(在夢詩以上)。所以activeWidth將有 值activeMaxAvailable。相同的規則適用於inactiveWidth

所以,當你建項目,design_bottom_navigation_item_max_widthdesign_bottom_navigation_active_item_max_width定義 到設計支持-lib的,將由我們定義的維度來代替。

驗證碼支持的最大選項(最大5)也。