android - 单击 TextView 时如何制作动画?

标签 android android-layout

如何让 TextView 在长按时表现得像 Button?

我使用这段代码创建了一个 TextView

<TextView
    android:text="Start"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btnStart"
    android:layout_alignParentBottom="true"
    android:gravity="center"

    android:clickable="true"
    android:focusable="true"
    android:longClickable="true"

    android:paddingTop="20dp"
    android:paddingBottom="20dp"
    android:textAllCaps="true"
    android:textStyle="bold"
    android:background="@drawable/rectangle"
    android:textSize="20sp"/>

但即使设置 android:longClickable="true" TextView 也不会在长按时显示气泡动画。预期结果是 here .

最佳答案

您需要设置可选择的背景:

<TextView
    android:background="?attr/selectableItemBackground"
    .../>

但是看到你已经使用了 android:background 你也可以使用 android:foreground 代替它:

<TextView
    android:foreground="?attr/selectableItemBackground"
    .../>

但要注意 android:foreground 只能从 API 21 (Lollipop) 开始工作。

如果您需要@drawable/rectangle 并支持Android <5.x,那么您必须创建一个自定义drawable 才能使其工作。查看RippleDrawable有关详细信息的文档。

关于android - 单击 TextView 时如何制作动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57452879/

相关文章:

android - 我无法在 Eclipse 中将 C/C++ Nature 添加到 Android 项目

Android 设置 OnClick 不工作/对话框不出现

android - 打开已设置名称的 "Add Contact Screen"

java - 在 android 中重绘位图的更快方法?

android - 如何将 View 置于屏幕外?

android - 由于 parent 的填充,未显示按钮高度

Android:在带有姓名和号码的 ListView 中设置联系人照片

android - 为什么将内容包装在多行 TextView 填充父项中?

android - ConstraintLayout 防止顶部和底部小部件重叠

android - 以编程方式设置 PreferenceScreen