【问题标题】:How can I change default background color of a selected ListView item in Android?如何更改 Android 中选定 ListView 项目的默认背景颜色?
【发布时间】:2014-07-15 20:38:45
【问题描述】:

我想更改 Android 导航抽屉中所选项目的默认颜色(蓝色)。我已经让它在过去的项目中工作,但我不能在我目前正在进行的项目中工作。

这是应用主题。

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="android:actionBarStyle">@style/AppTheme.ActionBar</item>
    <item name="android:activatedBackgroundIndicator">@drawable/activated_background</item>
    <item name="android:buttonStyle">@style/AppTheme.Button</item>
</style>

这是激活的背景可绘制对象。

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

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true" android:drawable="@drawable/list_selected"/>
    <item android:state_selected="true" android:drawable="@drawable/list_selected"/>
    <item android:state_pressed="true" android:drawable="@drawable/list_selected"/>
</selector>

最后,这是导航抽屉片段。

<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="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#cccc"
tools:context=".NavigationDrawerFragment" />

就像我说的,这对我以前有用。我能想到的项目之间的唯一区别是,以前,我的应用程序主题是从“Theme.AppCompat”继承的,而这个项目的主题是从“android:Theme.Holo.Light.DarkActionBar”继承的。另外,我在新项目中使用 v4 支持片段,而在另一个项目中我只使用标准片段。

任何建议将不胜感激。谢谢。

【问题讨论】:

    标签: android listview android-fragments


    【解决方案1】:

    编辑:

    似乎问题是如何保持选中 ListView 中的项目,而不是实际上如何设置所选项目的结果背景。

    查看这个很好的答案,了解如何在选中 ListView 项目后更改其背景:

    Android - Keep ListView's item highlighted once one has been clicked

    基本的想法是当项目被点击时,你改变它的背景(或其他任何东西)。棘手的部分是您必须让您的列表适配器记住选择的行。否则,当/如果您滚动列表/导航抽屉时,您将失去选择。

    原答案:

    您需要在AppTheme 中设置自定义ListView 样式,其中将android:listSelector 项设置为@drawable/activated_background

    如果您不希望整个应用程序使用该样式,只需在应用程序抽屉布局中为ListView 指定该样式,或者更简单地设置该ListViewandroid:listSelector 属性并且不要'甚至懒得定义样式。

    【讨论】:

    • 我已经试过了。这仅在按下列表项时更改颜色。按下后又变回蓝色。
    • 您的意思是您要指出当前选择了哪个项目,只要显示相应的活动/片段/等?
    • 是的,这正是我想要做的。
    • 更新答案..一秒
    • 我想通了。我添加了我的答案。不过感谢您的帮助!它比我预期的要简单得多。
    【解决方案2】:

    我想通了。我没有使用我的主题来尝试应用样式,而是为列表项创建了自己的 xml 布局,而不是使用默认设置并从那里设置选择器。

    这是列表项 xml 的样子。

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@android:id/text1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:textAppearance="?android:attr/textAppearanceListItemSmall"
      android:textColor="@drawable/list_txtcolor"
      android:gravity="center_vertical"
      android:paddingStart="?android:listPreferredItemPaddingStart"
      android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
      android:background="@drawable/activated_background"
      android:minHeight="?android:attr/listPreferredItemHeightSmall">
    </TextView>
    

    如您所见,我只是将项目背景设置为我的选择器。

    android:background="@drawable/activated_background"
    

    再一次,这是我的选择器。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-10
      • 1970-01-01
      • 2012-10-24
      相关资源
      最近更新 更多