2017-01-02 11 views
14

私はAndroidアプリを開発中で、デザインライブラリからBottomNavigationViewを実装しています。私は多くの例を見てきましたが、レイアウトに何が間違っているのか分かりません。 BottomNavigationViewは全角で表示されません。BottomNavigationViewはフルサイズではありません

もう1つの問題は、ステータスバーの色が適用されないことです。

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の間で変化します。定義した2つのアクションは、領域全体を水平に塗りつぶすのに十分ではありません。
は (サイドノートとして、また、ガイドラインに従ってビューは3と5アクション間含まれている必要があります。)

あなたはBottomNavigationViewの後ろにスペースを埋めるためにしたい場合は、あなたがするビューの背景色を設定することができます私はそれが完全な幅とバーが表示され、ユーザーが私をクリックしたときにそれが波及効果を維持します

android:background="@color/bottom_view_color" 

を使用することをお勧め

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

    // .... /> 
+0

Google Play Newstandではボトムナブが全幅に見えるので、レイアウトに問題があると考えました。私はすぐに3番目のメニュー項目を追加するつもりです。 – midhunhk

+0

@midhunhkはい、可能です。私はそのためのソリューションを追加しました。しかしAndyは正しいです、あなたは設計仕様に従うべきです。 –

5

:アイテムの背景と同じ色テムズ。

さらに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のいずれかになる必要があります。以上を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


これはマジックではありません。寸法は、名前と値が追加されているなぜ

は600dp

両方の次元である(BottomNavigationViewのメニューを表すために使用されるクラスである)BottomNavigationMenuView.javaによって使用されています。 は以下dimensに(常にactiveMaxAvailableの値を使用するために、私はmActiveItemMaxWidthにダミー値を設定

@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); 
     ..... 
    } 
    .... 
} 

以下のように、これらの値は、固定幅のビューを作成するために使用されているコード

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

あります上記)。したがってactiveWidthは の値がactiveMaxAvailableになります。同じルールがinactiveWidthに適用されます。

ですから、プロジェクトをビルドするとき、design_bottom_navigation_item_max_widthdesign_bottom_navigation_active_item_max_width設計支援-libのに を定義したが、私達によって定義された寸法によって置き換えられます。

コードはサポートされている最大オプション(最大5個)でも確認されています。

関連する問題