如何改变的ListView所选项目的颜色(这不是一个自定义的ListView) [英] How to change the color of selected item of ListView (which is not a custom ListView)

查看:78
本文介绍了如何改变的ListView所选项目的颜色(这不是一个自定义的ListView)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何更改自定义的ListView所选项目的颜色。但我不知道如何改变颜色正常的ListView。

我试过这样:


 < ListView的机器人:ID =@ ID /安卓名单
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:drawSelectorOnTop =假
    机器人:listSelector =@绘制/ listitemselector>


 <?XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
<项目
    机器人:STATE_ pressed =真
    机器人:可绘制=@色/ light_blue>< /项目>
< /选择>

但是,这只是改变了整个的ListView的颜色。我不想实现自定义的ListView只是选择颜色的变化。请提供了一个解决方案。


解决方案

其形状为背景试试吧,而不是简单的颜色。下面是我的一个项目一个例子:

listselector_blue.xml:

 <?XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
   <项目的android:STATE_ pressed =真正的机器人:可绘制=@绘制/ selector_state_blue/>
   <项目机器人:可绘制=@色/透明/>
< /选择>

selector_state_blue.xml:

 <形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:形状=矩形>    <梯度
        机器人:角=270
        机器人:ENDCOLOR =#FF91B2FF
        机器人:startColor =#4491B2FF/>    <角落的an​​droid:半径=10px的/>< /形状>

在ListView中活动的布局XML:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>    < ListView的机器人:ID =@ + ID / list1的
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:listSelector =@绘制/ listselector_blue/>< / LinearLayout中>

I know how to change the color of selected item of CUSTOM ListView. But i dont know how to change the color for normal ListView.

I had tried this :

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

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

But this just changes the color of Entire ListView. I dont want to implement CUSTOM ListView just for selector color change. Please provide a solution for this.

解决方案

Try it with a shape as the background, instead of a simple color. Here's an example from one of my projects:

listselector_blue.xml:

<?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/selector_state_blue"/>
   <item android:drawable="@color/transparent"/>
</selector>

selector_state_blue.xml:

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

    <gradient
        android:angle="270"
        android:endColor="#FF91B2FF"
        android:startColor="#4491B2FF" />

    <corners android:radius="10px" />

</shape>

Layout xml of the Activity with the ListView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView android:id="@+id/list1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:listSelector="@drawable/listselector_blue" />

</LinearLayout>

这篇关于如何改变的ListView所选项目的颜色(这不是一个自定义的ListView)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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