默認情況下呢,textview組件是沒有邊框的,那麼如果想在textview周圍添加邊框,該如何實現呢?
我們可以爲Textview設置一個背景Drawable,該Drawable是一個邊框,這樣不就實現了帶邊框的Textview麼。
下面是佈局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="帶邊框的文本"
android:background="@drawable/text1"/>
<TextView
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="圓角邊框,漸變背景的文本"
android:textSize="24sp"
android:background="@drawable/text2"
/>
</LinearLayout>
text1.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:color="#f00" android:width="4px"/>
<solid android:color="#0000"/>
</shape>
text2.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:topLeftRadius="20px"
android:topRightRadius="5px"
android:bottomLeftRadius="5px"
android:bottomRightRadius="20px"
/>
<stroke android:color="#f0f" android:width="4px"/>
<gradient android:startColor="#f00"
android:centerColor="#f0f"
android:endColor="#00f"
android:type="sweep"/>
</shape>
從上面可以得出,通過爲textview的android:background賦值,可以爲文本框增加大量的自定義的外觀,這種控制方式非常的靈活。
需要指出的是,表面上textview在使用該方式,但是由於edittext和button是,textview的子類,所以該方式同樣使用它們