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

时间:2012-02-12 16:00:32

标签: android css-selectors drawable

我有一些可点击的视图,我想设置列表单击时出现的默认可用背景(在ICS中是蓝色)。我试过把它作为背景:

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

但它不是默认的默认蓝色(我使用的是橙色)。 android上作为点击选择器默认使用的drawable是什么?

由于

9 个答案:

答案 0 :(得分:154)

这对我有用:

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

答案 1 :(得分:20)

它是list_selector_holo_dark或等效的全息灯版本;这些是Honeycomb及以上的默认值。 list_selector_background是非全息版本,用于Gingerbread及以下版本。

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

答案 2 :(得分:11)

如果您使用appcompat-v7,则可以使用?attr/selectableItemBackground

答案 3 :(得分: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

答案 4 :(得分:5)

类似于flx的答案的解决方案,但没有额外的属性定义。

用于前Holo设备的平台独立样式(在res\values\styles.xml中):

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

Holo设备的样式(API级别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>

答案 5 :(得分:4)

或者,

android:background="?android:attr/listChoiceBackgroundIndicator

如果您只是希望项目在点击时突出显示蓝色(或当前的默认值)。

答案 6 :(得分:4)

这适用于api 11及以上版本。但正如所指出的,它不适用于以前的版本。

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

这是一个让它在运行android的所有版本上运行的解决方案。

  1. 在colors.xml中添加适当的颜色,该颜色位于values文件夹中。它应该是这样的:

    <color name="white">#ffffff</color>
    <color name="blue">#7ecce8</color>
    
  2. 创建xml选择器文件。在这里我将其命名为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. 转到您的视图或按钮,并将新创建的button_selection.xml设置为其背景。

    android:background="@drawable/button_selection"
    

答案 7 :(得分:1)

设置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来查看结果。

答案 8 :(得分:0)

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