android textview设置圆角边框并且带点击效果
时间: 2023-08-18 17:08:33 浏览: 266
要实现这个效果,可以使用一个自定义的 Drawable 来实现 TextView 的背景,然后通过设置 Selector 来实现点击效果。下面是一个示例代码:
1. 定义圆角矩形 Drawable
```xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="8dp" />
<stroke
android:width="2dp"
android:color="#000000" />
</shape>
```
2. 定义 Selector
```xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="8dp" />
<stroke
android:width="2dp"
android:color="#FF0000" />
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="8dp" />
<stroke
android:width="2dp"
android:color="#000000" />
</shape>
</item>
</selector>
```
3. 在布局文件中使用 TextView
```xml
<TextView
android:id="@+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/myDrawable"
android:text="Hello, world!"
android:padding="8dp"
android:textColor="#000000" />
```
这样就可以实现一个带有圆角边框和点击效果的 TextView 了。
阅读全文
相关推荐

















