How to put border around textview?

被刻印的时光 ゝ 提交于 2020-04-19 08:57:19

问题


border.xml (in drawable folder)

<?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"/>

I am unable to get the border in the textview using the above code. The constraint layout width and height are set to match_parent.


回答1:


I think you're wrong in border.xml file.

Try doing a test with this:

<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>

if it works it means that something wrong in the syntax. (maybe badly used the selector)



来源:https://stackoverflow.com/questions/42387152/how-to-put-border-around-textview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!