【问题标题】:highlight selected item on listview突出显示列表视图上的选定项目
【发布时间】:2013-10-26 17:19:24
【问题描述】:

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

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

在列表视图上我添加了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”,我从here 找到了这个答案 但不知何故它出错了,上面写着No resource found that matches the given name,我认为缺少某些东西,所以我将其更改为 png 文件

无论如何,当我尝试运行它时,无论我是否选择它,它总是显示ic_launcher,它永远不会显示blue

【问题讨论】:

    标签: android listview android-listview


    【解决方案1】:

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

    这是一个关于how to change min. API level for your Android app in Eclipse的好答案

    【讨论】:

      【解决方案2】:

      这是我在 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 中添加这个android:theme="@style/mytheme" 到你的 ListView 类的活动中。

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-12-22
        • 1970-01-01
        • 1970-01-01
        • 2012-06-10
        • 1970-01-01
        • 1970-01-01
        • 2012-02-02
        • 1970-01-01
        相关资源
        最近更新 更多