可点击视图中默认选择器的背景色

54

我有一些可点击的视图,我想设置默认的可用背景,该背景在列表项被点击时显示(在ICS中是蓝色)。我尝试将其设置为背景:

我有一些可点击的视图,我想要设置默认的可用背景,在列表项被点击时会显示出来(在 ICS 中是蓝色)。我已经尝试将以下代码设置为背景:

android:background="@android:drawable/list_selector_background"

但默认情况下它不是我默认使用的那种蓝色(我使用的是橙色)。在Android上作为点击选择器时,默认使用哪个可绘制对象?

谢谢


15
使用 android:background="?android:attr/selectableItemBackground"。 - Adnan
9个回答

178
这对我有用:

这对我有用:

android:background="?android:attr/selectableItemBackground"

26
把它放到一个名为v11的文件夹里,就这么简单,欢迎来到Android;-) - Waza_Be
1
android:background="?android:attr/listChoiceBackgroundIndicator" 可以翻译为: - deadfish
9
在使用Android支持库时,使用?attr/selectableItemBackground也适用于2.x版本。在Gingerbread版本上,它会给我一个蓝色的背景(在模拟器上),在KitKat设备上则是浅灰色。 - Ravi K Thapliyal
3
好的,运作得很完美。你知道我怎样可以改变颜色吗? - Joaquin Iurchuk
如何强制使用其轻色版,而非暗色版? - android developer

21

这是list_selector_holo_dark或其等效的holo light版本;这些是Honeycomb及以上版本中的默认设置。 list_selector_background是非holo版本,在Gingerbread及以下版本中使用。

编辑:我相信(但无法确认),平台不可知的选择器是?android:attr/listSelector


2
很抱歉,这里没有一个。如果你想在所有版本的Android上使用ICS(蓝色)主题,你需要将可绘制资源复制到你自己的项目中。或者,如果你想在每个Android版本中使用默认主题,你需要创建两个可点击视图的版本,一个在\layout\中使用@android:drawable/list_selector_background,另一个在\layout-v11\中使用@android:drawable/list_selector_holo_dark - Alex Curran
2
顺便提一下,@android:drawable/list_selector_holo_dark 不是公共的,所以我不能在布局中使用它。 - Matroska
2
这样做就违背了使用默认Android资源的初衷,因为每次更新都会更新这些资源。我不想复制一个绿色选择器,然后发现他们在下一个版本中将默认的listView选择器更新为粉色。 - Buffalo
@Matroska list_selector_holo_dark在SDK中不可公开使用。您需要使用此网站生成资源:http://android-holo-colors.com/ - IgorGanapolsky
2
@Matroska 这是 android:attr/listChoiceBackgroundIndicator,可以通过在 sdk 源码的 res/values/themes.xml 中搜索 list_selector_holo_dark 来确认。android:attr/listSelector 是由 AbsListView 使用的属性值,用于检索 xml 值 android:listSelector,如果您将其用作属性的值,通常会在 TypedValue.getDrawable() 内抛出异常。 - Hai Zhang
显示剩余2条评论

12

如果你正在使用appcompat-v7,你可以直接使用?attr/selectableItemBackground


7
可以定制它,使颜色不同吗? - android developer

8

有一种方法可以将所有有效答案组合起来:

定义一个属性(例如在values/attrs.xml文件中):

<attr name="clickableItemBackground" format="reference"/>

在您的平台相关主题部分(例如在values/styles.xml或values/themes.xml中)声明此内容:
<style name="Theme.Platform" parent="@android:style/Theme.Whatever">
        <item name="clickableItemBackground">@android:drawable/list_selector_background</item>
</style>

在针对 API-11+ 的平台相关主题部分(例如在 values-v11/styles.xml 或 values-v11/themes.xml 中),声明以下内容:
<style name="Theme.Platform" parent="@android:style/Theme.Holo.Whatever">
        <item name="clickableItemBackground">?android:attr/selectableItemBackground</item>
</style>

然后在需要的地方使用?attr/clickableItemBackground


6

这是一种类似于flx答案的解决方案,但不需要额外的属性定义。

res\values\styles.xml中使用的适用于Holo之前设备的平台无关样式:

<style name="SelectableItem">
    <item name="android:background">@android:drawable/list_selector_background</item>
</style>

针对Holo设备的样式(API Level 14+)(位于res\values-v14\styles.xml中):

<style name="SelectableItem">
    <item name="android:background">?android:attr/selectableItemBackground</item>
</style>

将样式应用于需要的视图,例如:LinearLayout

<LinearLayout
    style="@style/SelectableItem"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
...
</LinearLayout>

终于有东西可以工作了,而不必创建不同的布局文件。 “background”和“attr”标签与“drawable”标签不兼容,后者对于选择器状态是必需的,因此本页上的所有其他解决方案都无法工作。 - lenooh

6

这在API 11及以上版本正常工作。但是如注所述,它在早期版本上无法运行。

android:background="?android:attr/selectableItemBackground"

这是一个在所有版本的安卓系统上运行的解决方案。
  1. Add the appropriate colors within the colors.xml which is located within your values folder. It should appear as such:

    <color name="white">#ffffff</color>
    <color name="blue">#7ecce8</color>
    
  2. Create an xml selector file. Here I named it button_selection.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/blue"/>         <!--pressed -->
    <item android:state_focused="true" android:drawable="@color/blue"/> <!-- focused -->
    <item android:drawable="@color/white"/> <!-- default -->
    </selector> 
    
  3. Go to your view or button and set the newly created button_selection.xml as its background.

    android:background="@drawable/button_selection"
    

4

或者,

android:background="?android:attr/listChoiceBackgroundIndicator

如果您只想在点击时使项目变为蓝色(或者当前默认颜色),请使用以下代码。


不,这在JellyBean设备上不会默认为list_selector_holo_dark。它仍然使用黄橙色(Gingerbread风格)来突出显示列表项。 - IgorGanapolsky
正常工作,emulator-api4.0-blueemulator-nexus4-api4.1.1-bluedevice-nexus4-api4.4.2-gray - deadfish

3

设置Background会限制您在以后选择背景颜色或可绘制物! 因此,我的建议是将此样式添加到您的styles.xml中:

<style name="TouchableView">
    <item name="android:foreground">?android:attr/selectableItemBackground</item>
</style>

而在每个视图中,您只需要添加以下内容:

            android:theme="@style/TouchableView"

就像任何视图一样!

    <TextView
        android:id="@+id/my_orders"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="0dp"
        android:theme="@style/TouchableView"
        android:background="@color/white"
        android:gravity="left"
        android:padding="10dp"
        android:text="@string/my_orders"
        android:textColor="@color/gray"
        android:textSize="14sp" />

不要忘记添加onClickListener以查看结果。


1
简而言之 - android:background="?selectableItemBackground";)

你如何在运行时解决这个问题? - TheRealChx101
你如何在运行时解决这个问题? - TheRealChx101

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接