如何设置高亮颜色ListView项? [英] How to set highlight color for ListView item?

查看:139
本文介绍了如何设置高亮颜色ListView项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要再拍高亮颜色ListView项。我使用的物品自定义适配器,我有以下code:

I have to make another highlight color for ListView item. I use custom adapter for items, and i have following code:

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    android:weightSum="1.0"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_weight="0.13"
        android:background="@drawable/booklist_header"
        android:orientation="horizontal"
        android:layout_height="0dp" >

        <ImageView
            android:id="@+id/imageViewBookListBack"
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:src="@drawable/back_button" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_weight="0.77"
        android:layout_height="0dp" >

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

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayoutBooklistAdwhirl"
        android:layout_width="fill_parent"
        android:layout_weight="0.1"
        android:layout_height="0dp" >
    </LinearLayout>

</LinearLayout>

$ C $下项目的布局:

Code for item layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageViewBookListItemImage"
        android:layout_width="120dp"
        android:layout_marginTop="15dp"
        android:layout_height="120dp" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="135dp"
        android:layout_marginTop="15dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textViewBookListItemTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textColor="#000000"
            android:textStyle="bold"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/textViewBookListItemAuthor"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textColor="#000000"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </LinearLayout>

</LinearLayout>

$ C $下选择:

Code for selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@color/green" /> <!-- focused -->
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/green" /> <!-- focused and pressed-->
    <item android:state_pressed="true" android:drawable="@color/green" /> <!-- pressed -->
    <item android:drawable="@color/green" /> <!-- default -->
</selector> 

$ C $下color.xml:

Code for color.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="green">#006400</color>
    <color name="white">#FFFFFF</color>


</resources>

不过,我有一个问题:ListView项没有通过点击变色!它有白色永远。我在哪里出错了?

But I have a problem: ListView item doesn't change color by click! It has white color always. Where have I made mistake?

推荐答案

这是你如何做到这一点:

This is how you do it:

首先,在你的的ListView ,把下面的:

First, in your ListView, put the following:

 android:listSelector="#00000000"

这使你listSelector(颜色,你通常会看到当您单击列表视图)透明。

This makes your listSelector (the color you normally see when you click the listview) transparent.

接下来,将的LinearLayout 您的项目布局,以这样的:

Next, set the LinearLayout of your item layout to this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    android:background="@drawable/selector"
    android:orientation="horizontal" >

我前几天有同样的问题,我花了年龄摸不着头脑。希望它为你工作!

I had the same problem a few days ago and it took me ages to figure this out. Hopefully it works for you!

这篇关于如何设置高亮颜色ListView项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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