TextView设置边框

在drawable文件夹下新建一个xml(text_view_shape.xml)文件,写入一下代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="2dp" />
    <stroke
        android:width="1dp"
        android:color="@color/colorAccent" />
    <padding
        android:bottom="2dp"
        android:left="4dp"
        android:right="4dp"
        android:top="2dp" />
</shape>
  • corners 是边角的弧度
  • stroke是边框的颜色和粗细
  • padding是边框距文字的距离

最后再布局文件里引用就可以了:

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="4dp"
    android:background="@drawable/text_view_shape"
    android:text="TextView" />
发布了107 篇原创文章 · 获赞 61 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/y_dd6011/article/details/104353681