日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁(yè) > 嵌入式 > 嵌入式軟件
[導(dǎo)讀] 1.如何改變item的背景色和按下顏色listview默認(rèn)情況下,item的背景色是黑色,在用戶點(diǎn)擊時(shí)是黃色的。如果需要修改為自定義的背景顏色,一般情況下有三種方法:1)設(shè)置listS

 1.如何改變item的背景色和按下顏色

listview默認(rèn)情況下,item的背景色是黑色,在用戶點(diǎn)擊時(shí)是黃色的。如果需要修改為自定義的背景顏色,一般情況下有三種方法:

1)設(shè)置listSelector

2)在布局文件中設(shè)置item的background

3)在adapter的getview中設(shè)置

這三種方法都能達(dá)到改變item默認(rèn)的背景色和按下顏色,下面來分別講解,但是在這之前需要先寫好selector.xml文件;

?

1
2
3
4
5
<!--?xml version="1.0" encoding="utf-8"?-->
    <item android:state_pressed="true" android:drawable="@color/light_blue"></item>
    <item android:state_pressed="false" android:drawable="@color/sgray"></item>
</selector>

在改變button或者listview的item默認(rèn)背景色,就可以用到selector。drawable可以設(shè)置為色彩資源,也可以設(shè)置為圖片資源。

1)設(shè)置listview的listSelector


?

1
2
<listview android:id="@+id/history_list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:divider="#565C5D" android:dividerheight="3dp" android:listselector="@drawable/selector" android:cachecolorhint="@android:color/transparent">
</listview>

2)在listitem的布局文件中設(shè)置background屬性,下面是listitem的布局文件


?

1
2
3
4
5
<!--?xml version="1.0" encoding="utf-8"?-->
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/selector">
    <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="歷史記錄" android:textcolor="#ffffff" android:textsize="20sp" android:layout_centerinparent="true">
     </textview>
</relativelayout>

3)在adapter的getView方法中設(shè)置

?

1
2
3
4
5
if(convertView ==null)
{
    convertView = LayoutInflater.from(context).inflate(R.layout.listitem, null);
}
convertView.setBackgroundResource(R.drawable.selector);

上述方法都能達(dá)到同樣的效果,就是改變item默認(rèn)的背景色和點(diǎn)擊時(shí)的背景顏色,第三種方法最靈活,如果listview的奇數(shù)行和偶數(shù)行需要設(shè)置為不同的selector,只能用第三種方法。

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉