日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

改变listview中item选中时文字的颜色

發(fā)布時間:2023/12/9 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 改变listview中item选中时文字的颜色 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

摘要?當listview的某個item選中時,默認有個選中的高亮顯示,如果你要自定義選中時的高亮顯示效果,可以在listview中設(shè)置屬性 android:listSelector="@drawable/item_selector" 其中 item_selector 是在drawable目錄下定義的一個xml文件,這種用于突出不同狀態(tài)下

? ?當listview的某個item選中時,默認有個選中的高亮顯示,如果你要自定義選中時的高亮顯示效果,可以在listview中設(shè)置屬性

1 android:listSelector="@drawable/item_selector"

? ?其中item_selector是在drawable目錄下定義的一個xml文件,這種用于突出不同狀態(tài)下顯示效果的xml文件我們稱之為selector:

1 2 3 4 5 6 7 <?xml version="1.0" encoding="utf-8"?> <selector ??xmlns:android="http://schemas.android.com/apk/res/android"> ????<item android:state_pressed="false" android:drawable="@*android:color/transparent" /> ????<item android:state_pressed="true" android:drawable="@drawable/grid_item_select_bg" /> ????<item android:state_selected="true" android:drawable="@drawable/grid_item_select_bg_night" /> </selector>

? ?上面這個selector定義了三種狀態(tài)下的顯示效果。

? ?但是如果我們想在listview的某個item選中時改變該item的某個textview的文字顏色,上面的辦法就行不通了。那該如何做呢?

? ?其實如果我們真正了解android:listSelector的含義的話,很容易實現(xiàn)上面的需求。

? ?我發(fā)現(xiàn)如果不在listview中設(shè)置listSelector,也就是將android:listSelector="@drawable/item_selector"去掉,而把item 的background屬性設(shè)為item_selector,會得到同樣的選中高亮效果。由此可見listview可以將自己的狀態(tài)(state_press、state_select、state_focus等)向內(nèi)傳遞,當然item本身也可以將這些狀態(tài)繼續(xù)傳遞給子view。

? ?受此啟發(fā),我們可以將需要高亮顯示文字顏色的TextView的textColor屬性也設(shè)置成selector的形式(沒想到吧textColor也可以用drawable定義)。

? ?假設(shè)item的xml原本定義為

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ????android:layout_width="fill_parent" ????android:layout_height="fill_parent" ????android:orientation="vertical"??????????????? ????> ????<TextView ????????android:id="@+id/txt" ????????android:layout_width="fill_parent" ????????android:layout_height="wrap_content" ????????android:text="@string/hello" ????????android:layout_margin="5dp"???????? ????????/> </LinearLayout>


? ?那么可以按照上面的辦法將item的xml改寫為:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ????android:layout_width="fill_parent" ????android:layout_height="fill_parent" ????android:orientation="vertical" ????android:background="@drawable/item_selector" <!-- item背景色變換 --> ????> ????<TextView ????????android:id="@+id/txt" ????????android:layout_width="fill_parent" ????????android:layout_height="wrap_content" ????????android:text="@string/hello" ????????android:layout_margin="5dp" ???????android:textColor="@drawable/item_text_selector" <!-- item文字顏色變換 --> ????????/> </LinearLayout>

? ?其中,item_text_selector.xml的源碼如下:

1 2 3 4 5 6 7 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> ????<item android:state_focused="true" android:color="#333333" /> <!-- focused --> ????<item android:state_pressed="true" android:color="#333333" /> <!-- pressed --> ????<item android:state_selected="true" android:color="#333333" /> <!-- pressed --> ????<item android:color="#f4f4f4" /> <!-- default --> </selector>

? ?經(jīng)過本人實際測試該方法可行,如果想更加可靠不妨給TextView 增加個屬性

1 android:duplicateParentState="true"

表示會跟隨ParentView的狀態(tài)來變化,其實沒加也不會有問題,因為默認狀態(tài)本來就是能傳遞的,只是在某些極端的情況下可以設(shè)置這個屬性做一層保險。

轉(zhuǎn)載于:https://www.cnblogs.com/xgjblog/p/4228691.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的改变listview中item选中时文字的颜色的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。