天天看點

如何為TextView添加邊框

首先大家需要知道一個概念:android:background的值是一個xml檔案。這就意味着如果我們想要一個框,就要定義一個xml布局檔案,然後把布局檔案的内容賦給TextView的background屬性。

操作步驟如下:

第一步:定義xml檔案,命名為setbar_bg.xml,并寫入如下代碼:

<?xml version="1.0" encoding="UTF-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android">

    //背景色
    <solid android:color="#00000000"/>

    //邊框寬度,顔色
    <stroke android:width="2dip" android:color="#ff000000" />

</shape>
           

第二部,把xml布局放入drawable檔案目錄下,同時賦給background屬性

android:background="@drawable/setbar_bg"
           

然後就成功了

繼續閱讀