【问题标题】:Android Activity ListView Highlight caseAndroid Activity ListView 高亮案例
【发布时间】:2014-09-02 21:45:38
【问题描述】:

这很奇怪。我已经tried this steps given.

但似乎没有运气。 每当我单击 ListView 内的项目时, 该项目以黑色突出显示。而不是蓝色。 我错了哪一部分?

这是我的 activity_chat1.xml 文件

<?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/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Available Peers"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pick one of the peers below : " />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="128dp"
        android:listSelector="@layout/list_selection"
        android:choiceMode="singleChoice" />

    <Button
        android:id="@+id/btn_startchat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start to Chat" />

</LinearLayout>

这是我的 list_selection.xml 文件:

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

这是我的 color.xml 文件:(在 res/values 目录下)

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="pressed_color">#4d90fe</color>
<color name="checked_color">#ffffff</color>
<color name="activated_color">#ffffff</color>
</resources>

【问题讨论】:

  • 你能不能把list_selection.xml放到drawable文件夹里?
  • 然后改成android:listSelector="@drawable/list_selection"
  • 有什么区别?内容还是一样的。 @user3249477

标签: java android xml listview highlight


【解决方案1】:

正如 cmets 所说,将 list_selection.xml 移动到 res/drawable 文件夹

同时更改列表的 listSelector:

android:listSelector="@drawable/list_selection"

最后,colors.xml 文件应该在 res/values 中,并且您引用 使用颜色

android:drawable="@color/pressed_color"

【讨论】:

  • 仍然没有给我确切的结果,@user3249477
猜你喜欢
  • 2011-09-07
  • 1970-01-01
  • 2014-10-24
  • 2013-08-07
  • 2023-03-05
  • 2013-06-23
  • 2013-11-04
  • 2015-02-20
  • 1970-01-01
相关资源
最近更新 更多