Android 處理OnItemClickListener時關(guān)于焦點顏色的設(shè)置問題
Android 處理OnItemClickListener時關(guān)于焦點顏色的設(shè)置問題
當(dāng)我們使用OnItemClickListener來改變Item以使其比較突出時,我們一般采用如下的代碼。
public void onItemClick(AdapterView<?> parent, View view, int arg2, long arg3) { //恢復(fù)每個單元格背景色 TextView categoryTitle; for(int i=0;i<parent.getCount();i++) { categoryTitle = (TextView) parent.getChildAt(i); categoryTitle.setTextColor(0XFFADB2AD); categoryTitle.setBackgroundDrawable(null); } //設(shè)置選擇單元格的背景色 categoryTitle=(TextView)view; categoryTitle.setTextColor(0XFFFFFFFF); categoryTitle.setBackgroundColor(R.drawable.categorybar_item_background);
這樣焦點才會出現(xiàn)這樣的效果:
如果我們新建一個文件夾比如人color和drawable,然后通過里面的xml文件來調(diào)用顏色,代碼如下。
?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) { //恢復(fù)每個單元格背景色 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); } //設(shè)置選擇單元格的背景色 categoryTitle=(TextView)view; categoryTitle.setTextColor(R.color.white); categoryTitle.setBackgroundColor(R.drawable.categorybar_
運行后的結(jié)果如圖:
也就是顏色更愿意接受的是直接給一個值,而不是通過xml文件來實現(xiàn)。
大小: 5.6 KB
大小: 4.8 KB
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Android Studio使用教程(六):Gradle多渠道打包
這篇文章主要介紹了Android Studio使用教程(六):Gradle多渠道打包,本文講解了友盟多渠道打包、assemble結(jié)合Build Variants來創(chuàng)建task、完整的gradle腳本等內(nèi)容,需要的朋友可以參考下2015-05-05Android獲取設(shè)備隱私 忽略6.0權(quán)限管理
這篇文章主要介紹了Android獲取設(shè)備隱私,忽略6.0權(quán)限管理,感興趣的小伙伴們可以參考一下2016-01-01Android Surfaceview的繪制與應(yīng)用
這篇文章主要介紹了Android Surfaceview的繪制與應(yīng)用的相關(guān)資料,需要的朋友可以參考下2017-07-07關(guān)于Android Fragment對回退棧的詳細理解
這篇文章主要介紹了Android Fragment的回退棧示例詳細介紹的相關(guān)資料,在Android中Fragment回退棧是由Activity管理的,每個Activity都有自己的回退棧,其中保存了已經(jīng)停止(處于后臺)的Fragment實例,需要的朋友可以參考下2016-12-12