2015-10-13 40 views
2

對不起,我的英語。android,NavigationView的setItemTextColor和setItemIconTinList沒有效果

我需要動態設置NavigationView的項目文本和項目圖標顏色,但是由於某種原因,這不起作用。

這將是一個錯誤或我做錯了什麼? XML效果很好,但是當我做下面的事情,而不是:

我的代碼:

navigationView = (NavigationView) findViewById(R.id.nav_view); 

     int[][] states = new int[][] { 
       new int[] { }, // default 
       new int[] { android.R.attr.state_focused, android.R.attr.state_pressed }, // pressed 
       new int[] { android.R.attr.state_selected } // selected 
     }; 

     int[] colors = new int[] { 
       colorDefault, 
       colorFocused, 
       colorSelected 
     }; 

     ColorStateList myList = new ColorStateList(states, colors); 
     navigationView.setItemTextColor(myList); 
     navigationView.setItemIconTintList(myList); 

出於某種原因,我只得到了第一個顏色:(

回答

0

這就像抓例外情況:您需要從最具體的案例開始,然後使用更一般的案例,您的情況是:

int[][] states = new int[][] { 
      new int[] { android.R.attr.state_selected } // selected 
      new int[] { android.R.attr.state_focused, android.R.attr.state_pressed }, // pressed 
      new int[] { }, // default 
    }; 

int[] colors = new int[] { 
     colorSelected, 
     colorFocused, 
     colorDefault 
}; 

這對你有用嗎?

+0

謝謝,但不是沒有任何反應:( – user5195185