如何让 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/