Mat*_*ska 49 android css-selectors drawable
我有一些可点击的视图,我想设置列表单击时出现的默认可用背景(在ICS中是蓝色).我试过把这个作为背景:
android:background="@android:drawable/list_selector_background"
Run Code Online (Sandbox Code Playgroud)
但它不是我默认的默认蓝色(我使用的是橙色).android上作为点击选择器默认使用的drawable是什么?
谢谢
Bog*_*lan 160
这对我有用:
android:background="?android:attr/selectableItemBackground"
Run Code Online (Sandbox Code Playgroud)
Ale*_*ran 20
它是list_selector_holo_dark等效的全息灯版本; 这些是Honeycomb及以上的默认值.list_selector_background是非全息版本,用于姜饼及以下.
编辑:我相信(但无法确认)平台不可知选择器是?android:attr/listSelector
有一种方法可以将所有有效答案组合在一起:
定义属性(例如,在values/attrs.xml中):
<attr name="clickableItemBackground" format="reference"/>
Run Code Online (Sandbox Code Playgroud)
在您的平台相关主题部分(例如,在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>
Run Code Online (Sandbox Code Playgroud)
在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>
Run Code Online (Sandbox Code Playgroud)
然后?attr/clickableItemBackground在需要的地方使用.
类似于flx的答案的解决方案,但没有额外的属性定义.
用于前Holo设备的平台独立样式(in res\values\styles.xml):
<style name="SelectableItem">
<item name="android:background">@android:drawable/list_selector_background</item>
</style>
Run Code Online (Sandbox Code Playgroud)
Holo设备的样式(API Level 14+)(in res\values-v14\styles.xml):
<style name="SelectableItem">
<item name="android:background">?android:attr/selectableItemBackground</item>
</style>
Run Code Online (Sandbox Code Playgroud)
将样式应用于所需视图,例如LinearLayout:
<LinearLayout
style="@style/SelectableItem"
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
在api 11及以上版本上可以正常工作。但如前所述,它将无法在以前的版本上运行。
android:background="?android:attr/selectableItemBackground"
Run Code Online (Sandbox Code Playgroud)
这是让它在所有运行android的版本上运行的解决方案。
在values文件夹中的colors.xml中添加适当的颜色。它应显示为:
<color name="white">#ffffff</color>
<color name="blue">#7ecce8</color>
Run Code Online (Sandbox Code Playgroud)创建一个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>
Run Code Online (Sandbox Code Playgroud)转到视图或按钮,然后将新创建的button_selection.xml设置为背景。
android:background="@drawable/button_selection"
Run Code Online (Sandbox Code Playgroud)