Android 如何使用自定义选择器制作涟漪效果
Android how to make the ripple effect with custom selector
我正在创建一个代表键盘的自定义视图,布局非常简单它只是 9 个 textView 放置在 table 布局中。我希望当我按下其中一个键时,会出现漂亮的圆形波纹效果。
这是我目前所做的
drawable/button_selector.xml(color/selected是淡蓝色)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/selected" android:state_pressed="true" android:state_selected="false"/>
</selector>
style.xml(我在attrs.xml里放了selectable_bg_borderless)
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
[....]
<item name="selectable_bg_borderless">@drawable/btn_selector</item>
</style>
<style name="AppTheme.Text.KeypadKey" parent="@style/AppTheme.Text">
[..]
<item name="android:background">?selectable_bg_borderless</item>
[..]
</style>
layout/view_keypad.xml
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0"
android:gravity="center">
<TextView
android:id="@+id/key_1"
style="@style/AppTheme.Text.KeypadKey"
android:text="1"/>
[....]
</TableRow>
然而,当我按下“1”时,确实出现了蓝色背景,但它是一个原始而简单的正方形。我如何实现这样的效果?
确保主题是 appcompat,只需将视图的背景 属性 更改为
android:background="?selectableItemBackgroundBorderless
我正在创建一个代表键盘的自定义视图,布局非常简单它只是 9 个 textView 放置在 table 布局中。我希望当我按下其中一个键时,会出现漂亮的圆形波纹效果。
这是我目前所做的
drawable/button_selector.xml(color/selected是淡蓝色)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/selected" android:state_pressed="true" android:state_selected="false"/>
</selector>
style.xml(我在attrs.xml里放了selectable_bg_borderless)
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
[....]
<item name="selectable_bg_borderless">@drawable/btn_selector</item>
</style>
<style name="AppTheme.Text.KeypadKey" parent="@style/AppTheme.Text">
[..]
<item name="android:background">?selectable_bg_borderless</item>
[..]
</style>
layout/view_keypad.xml
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0"
android:gravity="center">
<TextView
android:id="@+id/key_1"
style="@style/AppTheme.Text.KeypadKey"
android:text="1"/>
[....]
</TableRow>
然而,当我按下“1”时,确实出现了蓝色背景,但它是一个原始而简单的正方形。我如何实现这样的效果?
确保主题是 appcompat,只需将视图的背景 属性 更改为
android:background="?selectableItemBackgroundBorderless