如何在textview周围放置边框?

How to put border around textview?

border.xml(在可绘制文件夹中)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <shape xmlns:android="http://schemas.android.com/apk/ res/android">
    <stroke android:width="2dp"
            android:color="#000"/>

    <corners android:radius="10dp" />

    <padding android:left="10dp"
             android:top="5dp"
             android:right="10dp"
             android:bottom="5dp"/>

     </shape>
</selector>

activity_main.xml

<TextView
    android:id="@+id/textView"
    android:layout_width="100dp"
    android:layout_height="20dp"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:background="@drawable/border"
    android:text="TextView"
    android:textSize="20sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

我无法使用上面的代码在文本视图中获取边框。约束布局宽度和高度设置为match_parent。

我认为你在 border.xml 文件中是错误的。

尝试用这个做一个测试:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
   <solid android:color="#ffffff" />
   <stroke android:width="1dip" android:color="#4fa5d5"/>
</shape>

如果有效,则说明语法有问题。 (可能是选择器用错了)