如何显示textview是可点击的?
How to show textview is clickable?
我有五个文本视图,都是可点击的,如何给它们一个视觉效果,让用户可以识别它是可点击的。我已经放弃了在 textview 下划线的想法,所以还有其他可用的方法吗?
我的代码
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#94FFFF" /> <!-- pressed -->
<item android:state_focused="true"
android:color="#94FFFF" /> <!-- focused -->
<item android:color="#5C9E70" /> <!-- default -->
</selector>
您可以使用 StateListDrawable
。只需创建一个带有 selector
作为根标记的 xml 文件,并根据您想要的行为将其作为 textColor
或 backgroundColor
分配给您的 TextView
。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_activated="true"
android:state_pressed="true"
android:drawable="@drawable/pressed_img" />
<item
android:state_activated="true"
android:drawable="@drawable/pressed_img" />
<item
android:drawable="@drawable/normal_img" />
</selector>
此外,您还必须通过将 android:clickable
属性设置为 true
来使 TextView
可点击。
我最喜欢的是在 TextView 周围画一个圆圈,实际上使它成为一个包含 TextView 样式元素的按钮;
<Button
android:id="@+id/quit"
style="@android:style/Widget.TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Your Text"
android:paddingBottom="8dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:textColor="#ffffff"
android:background="@drawable/but_bg" />
其中 but_bg 是一个包含
的 .xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:type="radial" android:gradientRadius="820"
android:startColor="#df242424"
android:centerColor="#df343434"
android:endColor="#da444444"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#ffffff" />
<corners
android:radius="7dp" />
<padding
android:left="8dp"
android:top="8dp"
android:right="8dp"
android:bottom="8dp" />
</shape>
</item>
</selector>
使用其中的径向颜色还可以使它看起来更具塑料感。这看起来像
您还可以将 xml 可绘制对象应用于布局,以防您想要保留 TextView。然后你就这样包装它:
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/but_bg"
[...]>
<TextView
[...] />
</LinearLayout>
编辑:我刚刚意识到 Button
基本上是具有另一种样式的 TextView
子类。我没有用 TextView
标签测试过这个,但如果有人想把它保留为 TextView
.
,它应该以同样的方式工作
我有五个文本视图,都是可点击的,如何给它们一个视觉效果,让用户可以识别它是可点击的。我已经放弃了在 textview 下划线的想法,所以还有其他可用的方法吗?
我的代码
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#94FFFF" /> <!-- pressed -->
<item android:state_focused="true"
android:color="#94FFFF" /> <!-- focused -->
<item android:color="#5C9E70" /> <!-- default -->
</selector>
您可以使用 StateListDrawable
。只需创建一个带有 selector
作为根标记的 xml 文件,并根据您想要的行为将其作为 textColor
或 backgroundColor
分配给您的 TextView
。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_activated="true"
android:state_pressed="true"
android:drawable="@drawable/pressed_img" />
<item
android:state_activated="true"
android:drawable="@drawable/pressed_img" />
<item
android:drawable="@drawable/normal_img" />
</selector>
此外,您还必须通过将 android:clickable
属性设置为 true
来使 TextView
可点击。
我最喜欢的是在 TextView 周围画一个圆圈,实际上使它成为一个包含 TextView 样式元素的按钮;
<Button
android:id="@+id/quit"
style="@android:style/Widget.TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Your Text"
android:paddingBottom="8dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:textColor="#ffffff"
android:background="@drawable/but_bg" />
其中 but_bg 是一个包含
的 .xml<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:type="radial" android:gradientRadius="820"
android:startColor="#df242424"
android:centerColor="#df343434"
android:endColor="#da444444"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#ffffff" />
<corners
android:radius="7dp" />
<padding
android:left="8dp"
android:top="8dp"
android:right="8dp"
android:bottom="8dp" />
</shape>
</item>
</selector>
使用其中的径向颜色还可以使它看起来更具塑料感。这看起来像
您还可以将 xml 可绘制对象应用于布局,以防您想要保留 TextView。然后你就这样包装它:
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/but_bg"
[...]>
<TextView
[...] />
</LinearLayout>
编辑:我刚刚意识到 Button
基本上是具有另一种样式的 TextView
子类。我没有用 TextView
标签测试过这个,但如果有人想把它保留为 TextView
.