ssummeraw 2012-11-30 06:37 采纳率: 10%
浏览 9130
已采纳

Listview 中的ListSelector不能正常运行

我自定义了 listselector方法:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item 
        android:state_pressed="true"
        android:drawable="@drawable/bg_list_hover"></item>
    <item 
        android:state_focused="true"
        android:drawable="@drawable/bg_list"></item>
</selector>

在Listview中是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/bg_dasar" >
    <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="StoreCategory" 
                android:textSize="15sp"
                android:paddingLeft="10dip"
                android:paddingTop="10dip"
                android:paddingRight="10dip"
                android:textColor="@color/textcolor"/>

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:listSelector="@drawable/listselector">
    </ListView>

我的疑惑是当我不点击listview视图时,列表视图项就不会使用focused state的背景(@drawable/bg_list)。
请求大家的指点,谢谢!

  • 写回答

3条回答 默认 最新

  • nana4ever 2012-11-30 09:43
    关注

    在listselector 改成这样:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
        <item 
         android:state_pressed="true" 
         android:drawable="@drawable/bg_list_hover" />
     <item 
         android:drawable="@drawable/bg_list" />
    </selector>
    

    listview布局中:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@drawable/bg_dasar" >
        <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="StoreCategory" 
                    android:textSize="15sp"
                    android:paddingLeft="10dip"
                    android:paddingTop="10dip"
                    android:paddingRight="10dip"
                    android:textColor="@color/textcolor"/>
    
        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dip"
            **android:listSelector="@android:color/transparent"**>
        </ListView>
    
    </LinearLayout>
    

    把list selector添加到list item后台中:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="55dip"
        android:orientation="horizontal" 
        android:padding="15dip"
        android:gravity="center_vertical"
        **android:background="@drawable/listselector"**>
    
        <ImageView
            android:id="@+id/imageViewlogo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dip" />
    
        <TextView
            android:id="@+id/textViewcategory"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textColor="@color/textcolor" 
            android:textSize="15sp"/>
    
    </LinearLayout>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题