ListSelector适用于整个列表 [英] ListSelector applies to the entire list

查看:193
本文介绍了ListSelector适用于整个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的列表,像这样一个listselector。

 < ListView的机器人:ID =@ + ID /列表机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT机器人:layout_below =@ + ID /圆
    机器人:listSelector =#99000000机器人:可点击=真正的机器人:cacheColorHint =#00000000机器人:背景=#00000000>
< / ListView控件>
 

正如你所看到的android:listSelector =#99000000,但黑阿尔​​法颜色应用于整个列表,而不是选择的项目


因此​​,这是我现在有,但整个列表还是变黑

:: listview_background.xml

 < XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
  <项目安卓state_enabled =假的Andr​​oid版本:state_focused =真
        机器人:可绘制=@可绘制/ list_normal/>
  <项目的android:STATE_ pressed =真
        机器人:可绘制=@可绘制/ list_ pressed/>
  <项目安卓state_focused =真
        机器人:可绘制=@可绘制/ list_active/>
< /选择器>
 

:: colors.xml

 <资源>
    <绘制NAME =list_normal>#96FFFFFF< /绘制>
    <绘制NAME =list_active>#66000000< /绘制>
    <绘制NAME =list_ pressed>#CA000000< /绘制>
< /资源>
 

:: XML标记在我的名单

 安卓listSelector =@可绘制/ listview_background
 

解决方案

我有同样的问题。我有一个自定义背景图片,我不想不得不做出这样的背景图像的变种,因为这将是乏味的重新present所有不同状态。

所以,我想要做的明显的东西,有一个是覆盖在​​聚焦列表项的顶部,当用户点击回车键或任何一个半透明的酒吧,闪到pressed叠加颜色这是更引人注目的,有点更不透明。

解决的办法是远离这是指内部listSelector颜色任何@Color或@drawable。我创建了两个3×3像素.png文件。每个保存与伽玛层。在我的情况下,它是两个相同颜色的每个上下混合在GIMP的颜色层在不同的透明度。因此,当你选择一个项目你会得到25%的色彩覆盖,当你preSS它你会得到50%的色PNG格式。我把它们放在我可绘制的bg_list_item_ pressed.png和bg_list_item_highlighted.png

然后,我把我的列表选择器:

 < XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

  <! - 选择 - >
  <项目
    机器人:state_focused =真
    机器人:STATE_ pressed =假
    机器人:可绘制=@可绘制/ bg_list_item_highlighted/> <! -  @绘制/ tab_focus  - >

  <  - !pressed  - >
  <项目
    机器人:STATE_ pressed =真
    机器人:可绘制=@可绘制/ bg_list_item_ pressed/> <! -  @绘制/ tab_ preSS  - >

< /选择器>
 

然后我说我listSelector属性,我的ListView在我的布局XML:

 安卓listSelector =@可绘制/ list_selector
机器人:drawSelectorOnTop =真
 

现在它的工作原理正是我想要的工作。包括使用方向键来选择一行,并点击它进入。获取高亮和后续pressing颜色究竟如何,他们应该的。

I have a simple list with a listselector like so.

<ListView android:id="@+id/list" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:layout_below="@+id/round"
    android:listSelector="#99000000" android:clickable="true" android:cacheColorHint="#00000000" android:background="#00000000">
</ListView>

As you can see android:listSelector="#99000000" but the "black alpha" color is applied to the entire list, not the selected item.


So this is what I have now but the entire list still turns black

::listview_background.xml

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

::colors.xml

<resources>
    <drawable name="list_normal">#96FFFFFF</drawable>
    <drawable name="list_active">#66000000</drawable>
    <drawable name="list_pressed">#CA000000</drawable>
</resources>

::the xml tag in my list

android:listSelector="@drawable/listview_background"

解决方案

I had the same problem. I have a custom background image, and I don't want to have to make variants of that background image because that would be tedious to represent all the different states.

So I want to do the obvious thing, have a semi-transparent bar that is overlayed on top of the focused listitem and when the user taps the "enter" key or whatever, it flashes to the pressed overlay color which is more striking and somewhat more opaque.

The solution was to stay away from any @color or @drawable that refers to a color inside listSelector. I created two 3x3 pixel .png files. Each saved with the gamma layer. In my case it's two of the same color each mixed down in Gimp with a different transparency on the color layer. So when you select an item you get an overlay with 25% color, and when you press it you get a png with 50% color. I put them in my drawables as bg_list_item_pressed.png and bg_list_item_highlighted.png

Then I set my list selector to:

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

  <!-- Selected --> 
  <item 
    android:state_focused="true" 
    android:state_pressed="false" 
    android:drawable="@drawable/bg_list_item_highlighted" /> <!--  @drawable/tab_focus -->

  <!-- Pressed -->
  <item 
    android:state_pressed="true" 
    android:drawable="@drawable/bg_list_item_pressed" /> <!--  @drawable/tab_press -->

</selector> 

Then I added my listSelector attributes to my ListView in my layout xml:

android:listSelector="@drawable/list_selector"
android:drawSelectorOnTop="true"

Now it works exactly how I want it to work. Including using the D-pad to select a row, and click it with enter. Getting the highlighting and subsequent pressing colors exactly how they should be.

这篇关于ListSelector适用于整个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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