2017-08-28 5 views
0

に変更すると、タブごとに状態の色を変えることはできますか? BottomNavigationにセレクタを追加してBottomNavigationstate_checkedの色を変更することができます。Android - `BottomNavigationView`の` state_checked`色を

私が必要とするのは、各タブのstate_checkedの色を変更することです。 私は上の最初のアイコンをクリックすると、私のBottomNavigation私は...など赤に2番目のアイコンを変更色に、緑に

BottomNavigationView

<android.support.design.widget.BottomNavigationView 
    android:id="@+id/navigation" 
    android:layout_width="match_parent" 
    android:layout_height="120dp" 
    android:layout_gravity="bottom" 
    app:itemIconTint="@drawable/selector" 
    app:itemTextColor="@drawable/selector" 
    android:background="?android:attr/windowBackground" 
    app:menu="@menu/navigation" /> 

selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_checked="true" android:color="@color/green" /> 
<item android:color="@android:color/darker_gray" /> 
</selector> 
をアイコン state_checked色を変更する必要があります

今度はstate_checkedの色は緑で、デフォルトの色はグレーです。

答えて

0

あなたはとして動的にこれを行うことができます -

public static StateListDrawable selectorForColor (int normalColor,int pressedColor) { 
     StateListDrawable states = new StateListDrawable(); 
     states.addState (new int[]{ android.R.attr.state_pressed }, 
      new ColorDrawable(pressed)); 
     states.addState (new int[]{ }, new ColorDrawable(normal)); 
     return states; 
    } 

コールあなたonClick()BottomNavigationの項目内でこの方法を、あなたがこの項目に設定したい色を渡します。それぞれのindexの項目の色を持つ色のarrayを作成してBottomNavigationとすることができます。 この方法はreturndrawableオブジェクトになります。これはあなたの商品に設定することができますview

関連する問題