繁体   English   中英

Android ListView项目背景

[英]android listview items background

我需要在正常和选定状态下更改列表视图项目的背景。

我用了:

    <ListView
    android:id="@+id/resultLV"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"        
    android:listSelector="@drawable/list_selector"        
    android:scrollbars="none" />

其中list_selector.xml是:

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">

 <!-- Selector style for listrow -->
 <item android:drawable="@drawable/listoff" android:state_pressed="false"   
 android:state_selected="false"/>
 <item android:drawable="@drawable/liston" android:state_pressed="true"/>
 <item android:drawable="@drawable/listoff"/>

 </selector>

问题是,当活动开始时,listview项的背景不会更改为listoff的背景。但是,当我选择一个项目时,它正确地更改为liston的背景,而当发布时,它将更改为listoff的背景。

所以我的问题是在开始时,为什么它与listoff背景drawable一起不出现?

谢谢

将选择器添加到ListView项目而不是ListView本身,因为当存在带有ListItem的可点击对象时,click事件首先响应Items。 :)

您可以通过在ListView项的onClick方法和ListView的onItemClick中添加日志来验证这一点,您会注意到onClick事件将被称为跳过ListView的onItemClick。

很高兴为您提供帮助。 :)

您的xml选择器drawable.Instaed渐变颜色上的问题,请放置您的drawable图片。

请看看@这个drawables

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android" android:dither="true">

  <item android:state_pressed="true"><shape>
  <gradient android:angle="0" android:endColor="#0080FF" android:startColor="#0080FF" />        </shape></item>
    <item><shape>
      <gradient android:angle="0" android:endColor="#3B5998" android:startColor="#3B5998" />


        </shape></item>

</selector>

希望这个能对您有所帮助

试试这个

<!-- Tab widget design -->
<item android:drawable="@drawable/listpres" android:state_pressed="true" android:state_selected="false"></item>
<item android:drawable="@drawable/list" android:state_pressed="false" android:state_selected="false"></item>
<item android:drawable="@drawable/listpres" android:state_selected="true"></item>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM