Android 處理OnItemClickListener時關于焦點顏色的設置問題
當我們使用OnItemClickListener來改變Item以使其比較突出時,我們一般采用如下的代碼。
public void onItemClick(AdapterView<?> parent, View view, int arg2, long arg3) { //恢復每個單元格背景色 TextView categoryTitle; for(int i=0;i<parent.getCount();i++) { categoryTitle = (TextView) parent.getChildAt(i); categoryTitle.setTextColor(0XFFADB2AD); categoryTitle.setBackgroundDrawable(null); } //設置選擇單元格的背景色 categoryTitle=(TextView)view; categoryTitle.setTextColor(0XFFFFFFFF); categoryTitle.setBackgroundColor(R.drawable.categorybar_item_background); 這樣焦點才會出現這樣的效果:
如果我們新建一個文件夾比如人color和drawable,然后通過里面的xml文件來調用顏色,代碼如下。
?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:color="#ffffffff"/> <item android:state_pressed="true" android:color="#ffffffff"/> <item android:state_selected="true" android:color="#ffffffff"/> <item android:color="#ffabd2ad"/>
public void onItemClick(AdapterView<?> parent, View view, int arg2, long arg3) { //恢復每個單元格背景色 TextView categoryTitle; for(int i=0;i<parent.getCount();i++) { categoryTitle = (TextView) parent.getChildAt(i); categoryTitle.setTextColor(R.color.category_title_normal_background); categoryTitle.setBackgroundDrawable(null); } //設置選擇單元格的背景色 categoryTitle=(TextView)view; categoryTitle.setTextColor(R.color.white); categoryTitle.setBackgroundColor(R.drawable.categorybar_運行后的結果如圖:

也就是顏色更愿意接受的是直接給一個值,而不是通過xml文件來實現。
大小: 5.6 KB
大小: 4.8 KB
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答