7

私のようなアプリケーションにBottomNavigationViewを追加しました。Android:Bottom Navigation View - 選択したアイテムのアイコンを変更

main.xml

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

bottom_navigation_main.xml

<?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/action_favorites" 
     android:enabled="true" 
     android:icon="@drawable/ic_favorite_white_24dp" 
     android:title="@string/text_favorites" 
     app:showAsAction="ifRoom" /> 
    <item 
     android:id="@+id/action_schedules" 
     android:enabled="true" 
     android:icon="@drawable/ic_access_time_white_24dp" 
     android:title="@string/text_schedules" 
     app:showAsAction="ifRoom" /> 
    <item 
     android:id="@+id/action_music" 
     android:enabled="true" 
     android:icon="@drawable/ic_audiotrack_white_24dp" 
     android:title="@string/text_music" 
     app:showAsAction="ifRoom" /> 
</menu> 

をクリックしMainActivity

bottomNavigationView.setOnNavigationItemSelectedListener(
     new BottomNavigationView.OnNavigationItemSelectedListener() { 
      @Override 
      public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
       switch (item.getItemId()) { 
        case R.id.action_favorites: 
         //need change icon of favotites here. 
        case R.id.action_schedules: 

        case R.id.action_music: 

       } 
       return true; 
      } 
     }); 

選択した位置の下部ナビゲーションのアイコンを変更したいと思います。ユーザーが1つのアイテムをクリックすると、この機能をどのように実現できますか?

(別のものへのユーザーが1つのアイテムをクリックした場合、その後のアイコンの変更)

答えて

7

をimporveしようとするアイコンを変更する

item.setIcon(R.drawable.icon_name) 

を使用することができますアイコンの変更が選択されたときに変更する必要があります。あなたは、単に描画可能なフォルダや画像に描画可能セレクタを作成することができます

Menu menu = bottomNavigationView.getMenu(); 
menu.findItem(R.id.action_favorites).setIcon(favDrawable); 

switch (item.getItemId()) { 
       case R.id.action_favorites: 
        item.setIcon(favDrawableSelected); 
       case R.id.action_schedules: 
       case R.id.action_music: 
      } 
+0

「item」を宣言するコードを明示的に追加すると便利だと思います。 MenuItem item = menu.findItem(R.id.action_favorites); – larrytech

+0

私はこのコードを試してみますが、他のアイコンをクリックすると、アイコンを塗りつぶすか、塗りつぶしアイコンのようにデフォルトのアイコンが設定されません –

1

答えを見つけました。私たちは..あなたはonclickのアイコンをリセットする必要があり、その後、スイッチケースに使用すると、1つにだけあなたを設定する必要が答え

bottomNavigationView.setOnNavigationItemSelectedListener(
      new BottomNavigationView.OnNavigationItemSelectedListener() { 
       @Override 
       public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
        switch (item.getItemId()) { 
         case R.id.action_favorites: 
          //change the icon 
         item.setIcon(R.drawable.icon_name); 
         case R.id.action_schedules: 

         case R.id.action_music: 

        } 
        return true; 
       } 
      }); 
12

は、例えば確認(セレクタを作成し、各状態のために描画可能を指定ビュー

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/calender_green" android:state_checked="true"/> 
    <item android:drawable="@drawable/calender_black" android:state_checked="false"/> 
</selector> 
+7

これはプログラム的な方法よりも効果的です。指定する状態は実際には 'android:state_checked'ではなく' android:state_selected'です – Yuntao

+0

これをどこに適用しますか? –

0

で使用されるウィジェットの状態に応じて変更することができあなたが選択した項目のアイコンを変更するために上記のソリューションが動作しない場合は、あなたのコードに行の下に追加して、チェックを外した状態)

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/calender_green" android:state_checked="true"/> 
    <item android:drawable="@drawable/calender_black" android:state_checked="false"/> 
</selector> 
+1

うまくいかないようです –

3

bottomNavigationView.setItemIconTintList(null); 

これにより、選択したアイテムアイコンの色合い効果が無効になります。

私は同じ問題を抱えていました。チェック/選択時にBottomNavigationViewアイテムのアイコンを変更するセレクタドロウアブルを追加しました。

+0

Thnxをうまく処理しています。 –

関連する問題