码迷,mamicode.com
首页 > 其他好文 > 详细

ListView未选中时背景

时间:2014-10-17 12:03:31      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:android

想给ListView设置未选中时背景,结果发现需要同时在ListView中设置listSelector和在ListView的单项item的布局文件中设置background,原因来日再查,知情者可以留言,谢谢。

ListView项的布局代码如下:

 

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/search_layout"
        android:layout_marginBottom="0dp"
        android:layout_marginTop="15dp"
        android:cacheColorHint="@android:color/transparent"
        android:listSelector="@drawable/list_item_range_all_selector_with_outline
    </ListView>

 

单项item的部分布局文件如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/app_list_layout"
    android:layout_width="match_parent"
    android:layout_height="90dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="4dp"
    android:descendantFocusability="blocksDescendants" 
    android:background="@drawable/list_item_range_all_selector_with_outline">

 

list_item_range_all_selector_with_outline的内容如下:

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

    <item android:drawable="@drawable/range_bg_all" android:state_pressed="true"/>
    <item android:drawable="@drawable/list_bg"/>

</selector>


而list_bg是个点9图片,如下:

bubuko.com,布布扣

 

注意list_item_range_all_selector_with_outline,在两个文件上都要设置上,才能得到以下效果:

1、未选中时效果,背景图为list_bg.9.png

bubuko.com,布布扣

 


2、按住某一项时效果

bubuko.com,布布扣

 

 

 

如果只在ListView中设置了,那会出现以下情况:

bubuko.com,布布扣

 

而如果仅在单项item布局中设置了background,会出现以下情况:

bubuko.com,布布扣

ListView未选中时背景

标签:android

原文地址:http://blog.csdn.net/shy1987shy/article/details/40181763

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!