【问题标题】:Set color item listview xml设置颜色项listview xml
【发布时间】:2014-06-15 20:23:57
【问题描述】:

我有一个导航抽屉,当我单击列表视图的一个项目时,我希望它会改变颜色。所以我有这个 xml 代码。

<?xml version="1.0" encoding="utf-8"?>

    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/navigation_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <FrameLayout 
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <ListView 
    android:id="@+id/listview"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:textColor="@drawable/test"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:background="#2F2F2F"/> 

</android.support.v4.widget.DrawerLayout>

测试代码是

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_selected="true" android:color="@android:color/black"/>
    <item android:state_focused="true" android:color="@android:color/black"/>
    <item android:state_pressed="true" android:color="@android:color/black"/>
    <item android:color="@android:color/black"/>

</selector>

但是当我点击时保持默认颜色并且不会变成黑色。为什么?问题出在哪里,我该如何解决?提前致谢。

【问题讨论】:

  • 为什么都是black

标签: java android xml


【解决方案1】:

这是因为您更改的是 textView 的 textColor 而不是 ListView 内的视图。它不像 css。

您必须将 android:textColor="@drawable/test" 放入 TextView(或任何视图)

【讨论】:

  • 是的,这可行,但单击后颜色不会保留。为什么?我该如何解决?
  • 因为您可能需要一些状态,例如 state_active 或 state_activated,但我认为这只适用于 ToggleButton (developer.android.com/reference/android/graphics/drawable/…) 如果这不起作用,您可以添加视图状态,并且单击时,您将不得不手动更改颜色
【解决方案2】:

在您的列表视图中使用android:listSelector="color/black"Refer here

【讨论】:

    猜你喜欢
    • 2012-07-18
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    • 2013-08-06
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 2013-11-06
    相关资源
    最近更新 更多