2017-05-16 36 views
1

私は最近、AndroidのBottomNavigationViewコンポーネントを検討しました。私は4つのメニュー項目を持っており、現在、私のBottomNavigationViewの構成は以下のようになります。Android下部のナビゲーション変更テキストとアイコンの色合いを選択する

<android.support.design.widget.BottomNavigationView 
    android:id="@+id/activity_product_details_bottom_navigation_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    app:itemBackground="@color/colorPrimary" 
    app:itemIconTint="@color/menu_select_deselect" 
    app:itemTextColor="@color/menu_select_deselect" 
    app:menu="@menu/menu_product_details"/> 

私がしたいことは&、選択した要素の識別色で選択解除されている1つの。

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

<item 
    android:id="@+id/menu_product_details_home" 
    app:showAsAction="ifRoom" 
    android:enabled="true" 
    android:icon="@drawable/ic_home" 
    android:title="@string/menu_product_details_home" /> 

<item 
    android:id="@+id/menu_product_details_product_documents" 
    app:showAsAction="ifRoom" 
    android:enabled="true" 
    android:icon="@drawable/ic_product_documents" 
    android:title="@string/menu_product_details_product_documents" /> 

<item 
    android:id="@+id/menu_product_details_product_part_list" 
    app:showAsAction="ifRoom" 
    android:enabled="true" 
    android:icon="@drawable/ic_product_part_list" 
    android:title="@string/menu_product_details_product_part_list" /> 

<item 
    android:id="@+id/menu_product_details_product_service" 
    app:showAsAction="ifRoom" 
    android:enabled="true" 
    android:icon="@drawable/ic_product_service" 
    android:title="@string/menu_product_details_product_service" /> 
</menu> 

誰でもコードと間違っている何で私を助けることができるが、以下のように

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:color="@color/white" android:state_checked="true"/> 
    <item android:color="@color/tint_color_deselected"/> 
</selector> 

menu_product_details.xmlは次のように私はまた、RES /色のディレクトリに置かれmenu_select_deselect.xmlという名前の色の状態リストファイルを作成します他の3つの項目をクリックしても、最初の要素のみが白色で表示されるため、

+0

menu_product_details.xmlのすべてのアイテムは、android:enabled = "true"を保持していますか?言い換えれば、すべてのアイテムがメニューで有効になっていますか? – Ram

+0

はい、すべての項目がtrueに設定されています。android:enabled = "true" –

答えて

0

@drawableはなく自分の質問に答える

<android.support.design.widget.BottomNavigationView 
    android:id="@+id/activity_product_details_bottom_navigation_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    app:itemBackground="@color/colorPrimary" 
    app:itemIconTint="@drawable/menu_select_deselect" 
    app:itemTextColor="@drawable/menu_select_deselect" 
    app:menu="@menu/menu_product_details"/> 
+0

"@drawable"と "@color"の両方がうまく機能しています。 –

3

を@color描画可能な使用を参照しながら、私のコードに誤りが)onNavigationItemSelected(で戻り値を置き換えることだった以前の私は帰国して、下記偽、期待されたものが真実を返すのに対して、それは誰かにとって有益かもしれません。

bottomNavigation.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() { 
@Override 
public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
    switch (item.getItemId()) { 
    case R.id.menu_product_details_home: 
    break; 
    case R.id.menu_product_details_product_documents: 
    break; 
    case R.id.menu_product_details_product_part_list: 
    break; 
    case R.id.menu_product_details_product_service: 
    break; 
    } 
    return true; 
} 
}); 
0
Change your selector file like below 

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:color="@color/white" android:state_enabled="true" /> 
    <item android:color="@color/colorPrimaryDark" android:state_enabled="false" /> 
</selector> 

and yes reference @drawable instead of @color 
+0

これは間違っていますが、state_checkedでなくstate_enabledである必要があります – Kubicko

0

あなたは、アイコンを選択/

app:itemIconTint="" 
app:itemTextColor="" 

これらはBottomNavigationViewの2つの性質であり、あなたが描画可能なセレクタを追加して、XMLでそれを設定することができますを追加することによって、選択解除のテキストの色合いの色を変更することができますその中に。

の色だけでなく、選択した項目のアイコンを変更したい場合は、別の解決方法があります。あなたのBottomNavigationView xmlファイルから

app:itemIconTint="" 

を削除し、BottomNavigationViewが利用可能な場合、あなたのクラスに行下に追加:

bottomNavigationView.setItemIconTintList(null); 

これは、選択した項目のアイコンの色の色合いの効果を無効にして、セレクタごとにアイコンを変更しますドロアブル

私は同じ問題を抱えていました。チェック/選択時にBottomNavigationViewアイテムのアイコンを変更するセレクタドロウアブルを追加しました。各項目のセレクタドロウアブルは、BottomNavigationViewのメニューファイルにアイコンとして追加されます。

関連する問題