1. 文本框TextView
TextView直接繼承了View,它還是EditText、Button兩個UI組件類的父類。
TextView是顯示文本,EditText可以允許用戶編輯文本。
TextView還派生了一個CheckedTextView,增加了checked(可勾選)狀態。
另外,TextView還派生出Button類。
實例:不同顏色、字體、帶鏈接的文本
Xml代碼清單:
<?xml version="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 設置字號爲20pt,文本框結尾處繪製圖片 -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="我愛JAVA"
android:textSize="20pt"
android:drawableEnd="@drawable/ic_launcher"
/>
<!-- 設置中間省略,所有字母大寫 -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="雕欄玉砌應猶在,只是朱顏改。問君能有幾多愁?恰似一江春水向東流。hqsA"
android:textAllCaps="true"
android:ellipsize="middle"
/>
<!-- 對郵件、電話添加鏈接 -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="郵件:[email protected],電話:11011212098"
android:autoLink="email|phone"
/>
<!-- 設置文字顏色、大小,並使用陰影 -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="測試文字"
android:shadowColor="#00f"
android:shadowDx="10.0"
android:shadowDy="8.0"
android:shadowRadius="3.0"
android:textColor="#F00"
android:textSize="18pt"
/>
<!-- 測試密碼框 -->
<TextView
android:id="@+id/password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:password="true"
/>
<CheckedTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="可勾選的文本"
android:checkMark="@drawable/ok"
/>"
</LinearLayout>
JAVA代碼清單略
效果圖:
2.圓角邊框、漸變背景TextView
TextView是不帶邊框的,如果想爲TextView添加邊框,只能“曲線救國”——爲TextView設置一個背景,該DrawView只是一個邊框,就可實現帶邊框的TextView.
實例:
Xml界面佈局文件代碼清單:
<?xml version="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 通過android:background指定背景 -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="帶邊框的文本"
android:textSize="24pt"
android:background="@drawable/bg_border"
/>
<!-- 通過android:drawable繪製一張圖片 -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="圓角邊框,簡便背景的文本"
android:textSize="24pt"
android:background="@drawable/bg_border2"
/>
</LinearLayout>
上面代碼定義了兩個TextView,其中第一個指定背景,第二個指定使用圓角邊框、漸變背景。第一個文本框指定的背景是由xml文件定義的,將該文件放在drawable_mdpi文件夾內,該xml文件也可當成Drawable使用。代碼爲:
<?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="#f00" />
</shape>
第二個文本框指定背景由xml文件定義的,將該文件放在drawable_mdpi文件夾內,該xml文件也可當成Drawable使用。代碼爲:
<?xml version="1.0"encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 指定圓角矩形的4個圓角的半徑 -->
<corners android:topLeftRadius="20px"
android:topRightRadius="5px"
android:bottomRightRadius="20px"
android:bottomLeftRadius="5px"/>
<!-- 指定邊框線條的寬帶和顏色 -->
<stroke android:width="4px"android:color="#f0f"/>
<!-- 指定使用漸變背景色,使用sweep類型的漸變
顏色從紅色→綠色→藍色 -->
<gradient android:startColor="#f00"
android:centerColor="#0f0"
android:endColor="#00f"
android:type="sweep"/>
</shape>
效果: