安卓:整个ListView的焦点改变颜色,而不是仅仅的ListView子项 [英] Android: Entire ListView changes color on focus, not just ListView child item

查看:115
本文介绍了安卓:整个ListView的焦点改变颜色,而不是仅仅的ListView子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个选择申请一个ListView,以方便那些没有触摸屏来浏览我的应用程序。关键是,通过将选择到ListView,它似乎只背景颜色应用到整个列表,而不是项目里面它。

任何想法?下面是一些code:

 <的ListView
   机器人:ID =@机器人:ID /列表
   机器人:layout_width =FILL_PARENT
   机器人:layout_height =FILL_PARENT
   机器人:listSelector =@可绘制/ listselector
   />

   < TextView的机器人:ID =@机器人:ID /空
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=没有客户呢。
        />
 

listselector.xml在绘制文件夹:

 < XML版本=1.0编码=UTF-8&GT?;
<选择
    机器人:ID =@ + ID / myselector
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <! - 非重点国家 - >
    <项目
        机器人:state_focused =假
        机器人:state_selected =假
        机器人:STATE_ pressed =假
        机器人:可绘制=@色/深蓝色/>
    <项目
        机器人:state_focused =假
        机器人:state_selected =真
        机器人:STATE_ pressed =假
        机器人:可绘制=@色/绿色/>

    <! - 聚焦国家 - >
    <项目
        机器人:state_focused =真
        机器人:state_selected =假
        机器人:STATE_ pressed =假
        机器人:可绘制=@色/绿色/>
    <项目
        机器人:state_focused =真
        机器人:state_selected =真
        机器人:STATE_ pressed =假
        机器人:可绘制=@色/绿色/>

    <  - !pressed  - >
    <项目
        机器人:STATE_ pressed =真
        机器人:可绘制=@色/绿色/>
< /选择器>
 

解决方案

你看到的聚焦颜色被应用到整个列表的原因是因为你已经直接引用的重点和pressed状态的颜色值。有一个在Android的V2.3及以下,其中的颜色绘制不履行其边界在这种情况下,一个已知的bug。

要解决这个问题,你可以创建一个使用所需的颜色和引用,而不是形状绘制。

例如定义可绘制/ list_selector_focused.xml作为形状:

 <形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
       机器人:形状=矩形>

    [固体机器人:颜色=@色/绿色/>

< /形状>
 

,然后引用,在选择器:

 <项目安卓state_focused =真
      机器人:state_selected =真
      机器人:STATE_ pressed =假
      机器人:可绘制=@可绘制/ list_selector_focused/>
 

I am trying to apply a selector to a ListView to make it easy for those without touch screens to navigate my app. The thing is, by applying the selector to the ListView, it only seems to apply the background colors to the entire list, not the items inside of it.

Any ideas? Here's some code:

    <ListView
   android:id="@android:id/list"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:listSelector="@drawable/listselector"
   />

   <TextView android:id="@android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="There are no Clients yet."
        />

listselector.xml in drawable folder:

    <?xml version="1.0" encoding="utf-8"?>
<selector
    android:id="@+id/myselector"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Non focused states -->
    <item
        android:state_focused="false"
        android:state_selected="false"
        android:state_pressed="false"
        android:drawable="@color/darkblue" />
    <item
        android:state_focused="false"
        android:state_selected="true"
        android:state_pressed="false"
        android:drawable="@color/green" />

    <!-- Focused states -->
    <item
        android:state_focused="true"
        android:state_selected="false"
        android:state_pressed="false"
        android:drawable="@color/green" />
    <item
        android:state_focused="true"
        android:state_selected="true"
        android:state_pressed="false"
        android:drawable="@color/green" />

    <!-- Pressed -->
    <item
        android:state_pressed="true"
        android:drawable="@color/green" />
</selector>

解决方案

The reason you're seeing the focused colour being applied to the whole list is because you have directly referenced a colour value for the focused and pressed states. There is a known bug in Android v2.3 and below where the colour drawable doesn't honour its bounds in these circumstances.

To fix this issue you can create a shape drawable using the desired colours and reference that instead.

e.g define the shape in 'drawables/list_selector_focused.xml' as:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">

    <solid android:color="@color/green" />

</shape>

And then reference that in the selector:

<item android:state_focused="true"
      android:state_selected="true"
      android:state_pressed="false"
      android:drawable="@drawable/list_selector_focused" />

这篇关于安卓:整个ListView的焦点改变颜色,而不是仅仅的ListView子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆