默认情况下,TextView是不带边框的,如果想为TextView添加对话框只能为TextView设置一个背景Drawable,该Drawable只是一个边框,还可以指定渐变背景。这样子就可以为TextView添加边框了。
/TextViewDemo/res/layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.textviewdemo.MainActivity" >
<!-- 通过android:background指定背景 -->
<TextView
android:id="@+id/textview01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:padding="5dip"
android:background="@drawable/bg_border"
android:text="带边框的文本"
/>
<TextView
android:id="@+id/textview02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:padding="12dip"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:background="@drawable/bg_border2"
android:text="圆角边框文本"
/>
</LinearLayout>
1、带边框的TextView
/TextViewDemo/res/drawable-mdpi/bg_border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 设置背景色为透明 -->
<solid
android:color="#0000"
/>
<stroke
android:width="4px"
android:color="#000"
/>
</shape>
2、带圆角边框的TextView
/TextViewDemo/res/drawable-mdpi/bg_border2.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 指定圆角矩形的4个圆角半径 -->
<corners
android:topLeftRadius="8px"
android:topRightRadius="8px"
android:bottomLeftRadius="8px"
android:bottomRightRadius="8px"
/>
<!-- 指定边框线条的宽度和颜色 -->
<stroke
android:width="4px"
android:color="#f0f"
/>
</shape>