有没有办法改变android中所有列表视图的默认按下颜色?

时间:2015-04-20 19:51:23

标签: android listview

我正在开发的应用程序有几个项目列表(包括一个在NavigationDrawer中,一个在对话框中等)。

当用户按下棒棒糖前智能手机上列表中的一个项目时,背景会变为淡蓝色(包括PreferenceActivity上的首选项)。然而,在棒棒糖设备上,背景变为浅灰色,具有美丽的波纹效果。我想要的是为所有设备提供相同的浅灰色(不需要涟漪效果)。

我似乎有一些人在设置ListView项目设置背景的样式,但我需要一种方法来同时设置所有这些。

ps:我的应用使用Theme.AppCompat.Light.NoActionBar作为基本主题。

2 个答案:

答案 0 :(得分:2)

使用以下内容创建文件res / drawable / listitem_background.xml:

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

@color/purple_dark替换为您选择的颜色。

然后,在您的主题中,添加以下行:

<item name="android:activatedBackgroundIndicator">@drawable/listitem_background</item>

编辑:

要保留棒棒糖效果和颜色,而不是将主题放在值文件夹中,请将其放在values-v22

答案 1 :(得分:1)

定义类似这样的样式资源文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyTheme" parent="android:Theme.AppCompat.Light.NoActionBar">
        <item name="android:listViewStyle">@style/MyListViewStyle</item>
    </style>

    <style name="MyListViewStyle" parent="android:Widget.ListView">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <!-- Other stuff you need -->
    </style>
</resources>

在你的清单集中

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/MyTheme">

现在,您的所有ListView看起来都像默认情况下定义的样式。如果你愿意,你当然可以改变其中一些。

您可以找到更复杂但非常有用的示例HERE