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

Android 可单击视图中的默认选择器背景,android,css-selectors,drawable,Android,Css Selectors,Drawable,我有一些可单击的视图,我想设置列表单击时显示的默认可用背景(在ICS中为蓝色)。我尝试将以下内容作为背景: android:background="@android:drawable/list_selector_background" 但它不是默认的蓝色(我使用的是橙色)。android上默认使用的drawable作为点击选择器是什么 谢谢这是列表选择器\u全息图\u暗版或等效全息图光版;这些是Honeycom和更高版本中的默认设置列表\选择器\背景是非全息版本,用于姜饼及以下食品 编辑:我相

我有一些可单击的视图,我想设置列表单击时显示的默认可用背景(在ICS中为蓝色)。我尝试将以下内容作为背景:

android:background="@android:drawable/list_selector_background"
但它不是默认的蓝色(我使用的是橙色)。android上默认使用的drawable作为点击选择器是什么


谢谢

这是
列表选择器\u全息图\u暗版
或等效全息图光版;这些是Honeycom和更高版本中的默认设置<代码>列表\选择器\背景是非全息版本,用于姜饼及以下食品

编辑:我相信(但无法确认)平台不可知选择器是

这对我有效:

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

android:background="?android:attr/listChoiceBackgroundIndicator

如果您只希望项目在单击时高亮显示蓝色(或当前默认值)。有一种方法可以组合所有有效答案:

定义属性(例如,在values/attrs.xml中):


在平台相关主题部分(例如,在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>
<style name="Theme.Platform" parent="@android:style/Theme.Holo.Whatever">
        <item name="clickableItemBackground">?android:attr/selectableItemBackground</item>
</style>

@android:drawable/list\u selector\u后台
在api-11+的平台相关主题部分(例如在values-v11/styles.xml或values-v11/themes.xml中)声明:

<style name="Theme.Platform" parent="@android:style/Theme.Whatever">
        <item name="clickableItemBackground">@android:drawable/list_selector_background</item>
</style>
<style name="Theme.Platform" parent="@android:style/Theme.Holo.Whatever">
        <item name="clickableItemBackground">?android:attr/selectableItemBackground</item>
</style>

?android:attr/selectableItemBackground

然后在需要时使用attr/clickableItemBackground。

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

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

将样式应用于所需视图,例如,
LinearLayout

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

...

这在api 11及以上版本上运行良好。但正如前面提到的,它在以前的版本上不起作用

android:background="?android:attr/selectableItemBackground"
这里有一个解决方案,可以让它在运行android的所有版本上运行

  • 在values文件夹中的colors.xml中添加适当的颜色。它应该是这样出现的:

    <color name="white">#ffffff</color>
    <color name="blue">#7ecce8</color>
    

  • 如果您使用的是
    appcompat-v7
    ,则只需使用
    ?attr/selectableItemBackground

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

    <style name="SelectableItem">
        <item name="android:background">@android:drawable/list_selector_background</item>
    </style>
    
    <style name="SelectableItem">
        <item name="android:background">?android:attr/selectableItemBackground</item>
    </style>
    
    <style name="TouchableView">
        <item name="android:foreground">?android:attr/selectableItemBackground</item>
    </style>
    
    喜欢任何视图

        <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
    以查看结果。

    简而言之-
    android:background=“?selectableitemcondground”
    ;)

    哪一个是默认的独立于主题的选择器?恐怕没有。如果你想在所有版本的Android上使用ICS(蓝色),你必须将可绘制资源复制到你自己的项目中。或者,如果你想为每个版本的Android使用默认版本,你必须创建两个版本的可点击视图,一个在
    \layout\
    中使用
    @android:drawable/list\u selector\u background
    和一个在
    \layout-v11\
    中使用
    @android:drawable/list\u selector\u holo\u dark
    顺便说一句,@android:drawable/list\u selector\u holo\u dark不是公共的,所以我不能在布局中使用它。这违背了使用默认设置的目的Android资源,每次更新都会更新。我不想复制一个绿色选择器,然后看到他们在下一个版本中将默认的listView选择器更新为粉红色。@Matroska它是
    android:attr/listChoiceBackgroundIndicator
    ,这可以通过在sdk source
    res/values/themes.xml
    中搜索
    list\u selector\u holo\u dark
    来确认
    android:attr/listSelector
    AbsListView
    用于检索xml值的属性值
    android:listSelector
    ,如果将其用作属性值,通常会在
    TypedValue.getDrawable()
    内引发异常。将其放入v11文件夹中。这么简单,欢迎使用Android;-)或者`android:background=“?android:attr/listChoiceBackgroundIndicator”`使用
    ?attr/selectableItemBackground
    和android支持库在2.x版上也可以使用。在姜饼上,它给了我一个蓝色的背景(在模拟器上),在KitKat设备上给了我一个浅灰色的背景。您知道如何更改颜色吗?如何强制它使用浅色版本而不是深色版本?不,这不会默认在JellyBean设备上列出\u选择器\u全息\u深色。仍然突出显示黄橙色(姜饼风格)的列表项。效果很好,
    emulator-api4.0-blue
    emulator-nexus4-api4.1.1-blue
    device-nexus4-api4.4.2-gray
    使用android:background=“?android:attr/selectableItemBackground”最后是可以工作的东西,无需创建不同的布局文件。“background”和“attr”标记与选择器状态所需的“drawable”标记不兼容,因此此页面上的所有其他解决方案都不兼容。是否可以对其进行自定义,以便颜色不同?
                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" />