更改列表视图行背景颜色

时间:2015-04-15 19:07:15

标签: android xml listview navigation-drawer

我有一个导航抽屉活动。默认选择的项目颜色为蓝色,我想将其更改为绿色。

这是我的fragment_navigation_drawer.xml:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="#19A3A3"
android:dividerHeight="1dp"
android:background="@drawable/background"
android:textColor="@color/primary_material_dark"
tools:context=".NavigationDrawerFragment"
/>

我在drawables中创建了background.xml:

    <?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_hovered="true" android:state_activated="true" android:state_checked="true" android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="#00FF00" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/material_blue_grey_900" />
        </shape>
    </item>
</selector>

当我执行应用程序时,所选列表视图项仍为蓝色。

任何解决它的想法?

1 个答案:

答案 0 :(得分:0)

<item android:state_hovered="true" android:state_activated="true"
      android:state_checked="true" android:state_pressed="true">...</item>

当您添加上述所有状态时,实际上意味着仅在每个语句解析为true时才选择该项。换句话说,您可以将状态视为AND'ed。您似乎认为它们是OR'ed。

不同地说,你现在所读的内容是:“如果(且仅当)元素悬停并激活并选中并按下,则选择此项目。”

首先尝试只设置一个状态来确认我的陈述(甚至没有):

<item android:state_activated="true">...</item>

如果您希望其他状态具有相同的行为,则必须为每个状态添加新的<item>...</item>条目。

相关问题