【问题标题】:Android how to make the ripple effect with custom selectorAndroid如何使用自定义选择器制作涟漪效果
【发布时间】:2018-02-04 18:47:29
【问题描述】:

我正在创建一个代表键盘的自定义视图,布局非常简单,它只是将 9 个 textView 放置在表格布局中。我希望当我按下其中一个键时会出现漂亮的圆形波纹效果。

这是我到目前为止所做的

drawable/button_selector.xml(颜色/选中的是浅蓝色)

<?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>

布局/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”时,我确实有一个蓝色背景出现,但它是一个原始而简单的正方形。我如何归档这样的东西?

【问题讨论】:

  • 尝试将按钮的背景更改为 android:background="?selectableItemBackgroundBorderless"
  • 请注意,您必须为此使用应用战斗主题
  • 呃...是的,有效
  • 我应该把它放在答案中,然后你将它标记为已解决,以便它可以帮助其他人吗?
  • 是的,我已经创建了一个 values-v21/style.xml 并用 ?android:selectableItemBackgroundBorderless 覆盖

标签: android textview selector ripple


【解决方案1】:

确保主题是 appcompat,只需将视图的背景属性更改为

android:background="?selectableItemBackgroundBorderless

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 2017-07-29
    相关资源
    最近更新 更多