繁体   English   中英

在列表视图上突出显示选中的项目

[英]highlight selected item on listview

在有人将此问题标记为重复之前,我必须说我已经知道这个问题已经被问过几次了,但是每次尝试都总是失败。

所以我在左边有一个日期列表,我想在右边显示数据,我想做一个可以显示选择哪个项目的列表视图,这样我就知道我选了哪个日期。 到目前为止,我已经尝试过了;

在列表视图上,我添加了android:choiceMode="singleChoice" ;

<ListView
        android:id="@+id/listTglRMPasien"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnObatAlkes"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true" 
        android:choiceMode="singleChoice">
</ListView>

在列表视图的项目上,我添加了android:background="@drawable/list_selector" ;

<?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" >

    <TextView 
        android:id="@+id/txtListRekamMedikTanggal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/emptyString"
        android:textAppearance="?android:attr/textAppearanceLarge" 
        android:background="@drawable/list_selector"/>

    <TextView
        android:id="@+id/txtListRekamMedikTabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/emptyString"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:visibility="gone" />

</LinearLayout>

并在list_selector上

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

请注意,ic_launcher和blue这里是png文件,应该是“ @ drawable / listitem_pressed”和“ @ drawable / solid_white”,我从这里找到了这个答案,但是不知何故出现了错误,它说No resource found that matches the given name ,我认为缺少某些内容,因此将其更改为png文件

无论如何,当我尝试运行此命令时,无论是否选择它始终显示ic_launcher ,从不显示blue

显然是因为我的API级别太低而发生的,所以我将其从API级别8更改为API级别14。

这是有关如何更改最小值的很好的答案 Eclipse中Android应用的API级别

这是我在ListView上所做的。 res/values文件夹上创建您自己的主题,可以将其命名为mytheme.xml。 这是我在mytheme.xml上添加的内容

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="mytheme" parent="android:Theme.Holo.Light">
   <item name="android:textColor">#fff</item>
</style>
</resources>

然后在Manifest中的ListView类的活动中添加此android:theme="@style/mytheme"

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM