UI组件之TextView及其子类(一)TextView和EditText
先來(lái)整理一下TexView,EditView的用法。
Textview是最基本的組件,直接繼承了View,也是眾多組件的父類,所以了解她的屬性會(huì)對(duì)學(xué)習(xí)其他組件很有幫助。
TextView的屬性:
? ? ? ?android:autoLink ? 設(shè)置是否當(dāng)文本為URL鏈接/email/電話號(hào)碼/map時(shí),文本顯示為可點(diǎn)擊的鏈接。可選值(none/web/email/phone/map/all)
android:autoText 如果設(shè)置,將自動(dòng)執(zhí)行輸入值的拼寫(xiě)糾正。此處無(wú)效果,在顯示輸入法并輸入的時(shí)候起作用。
android:bufferType 指定getText()方式取得的文本類別。選項(xiàng)editable 類似于StringBuilder可追加字符, 也就是說(shuō)getText后可調(diào)用append方法設(shè)置文本內(nèi)容。spannable 則可在給定的字符區(qū)域使用樣式,參見(jiàn)這里1、這里2。
android:capitalize 設(shè)置英文字母大寫(xiě)類型。此處無(wú)效果,需要彈出輸入法才能看得到,參見(jiàn)EditView此屬性說(shuō)明。 none不轉(zhuǎn)換,sentence每個(gè)句子的首字母大寫(xiě),words每個(gè)單詞字母大寫(xiě),characters每個(gè)字母都大寫(xiě)
android:cursorVisible 設(shè)定光標(biāo)為顯示/隱藏,默認(rèn)顯示。
android:digits 設(shè)置允許輸入哪些字符。如“1234567890.+-*/% ()”?
設(shè)置圖片和文本的位置關(guān)系:
android:drawableBottom 在text的下方輸出一個(gè)drawable,如圖片。如果指定一個(gè)顏色的話會(huì)把text的背景設(shè)為該顏色,并且同時(shí)和background使用時(shí)覆蓋后者。
android:drawableLeft 在text的左邊輸出一個(gè)drawable,如圖片。
android:drawablePadding 設(shè)置text與drawable(圖片)的間隔,與drawableLeft、drawableRight、drawableTop、drawableBottom一起使用,可設(shè)置為負(fù)數(shù),單獨(dú)使用沒(méi)有效果。
android:drawableRight在text的右邊輸出一個(gè)drawable。
android:drawableTop 在text的正上方輸出一個(gè)drawable。
android:editable設(shè)置是否可編輯。
android:editorExtras設(shè)置文本的額外的輸入數(shù)據(jù)。
android:ellipsize 設(shè)置當(dāng)文字過(guò)長(zhǎng)時(shí),該控件該如何顯示。有如下值設(shè)置:”start”—?省略號(hào)顯示在開(kāi)頭;”end”——省略號(hào)顯示在結(jié)尾;”middle”—-省略號(hào)顯示在中間; ”marquee” ——以跑馬燈的方式顯示(動(dòng)畫(huà)橫向移動(dòng))
android:freezesText 設(shè)置保存文本的內(nèi)容以及光標(biāo)的位置。
android:gravity 設(shè)置文本位置,是指控件中文本的位置,區(qū)分layout_gravity屬性,如設(shè)置成“center”,文本將居中顯示。
android:hintText 為空時(shí)顯示的文字提示信息,可通過(guò)textColorHint設(shè)置提示信息的顏色。此屬性在EditView中使用,但是這里也可以用。
android:imeOptions ?附加功能,設(shè)置右下角IME動(dòng)作與編輯框相關(guān)的動(dòng)作,如actionDone右下角將顯示一個(gè)“完成”,而不設(shè)置默認(rèn)是一個(gè)回車符號(hào)。這個(gè)在EditView中再詳細(xì)說(shuō)明,此處無(wú)用。
android:imeActionId 設(shè)置IME動(dòng)作ID。
android:imeActionLabel 設(shè)置IME動(dòng)作標(biāo)簽。
android:includeFontPadding 設(shè)置文本是否包含頂部和底部額外空白,默認(rèn)為true。
android:inputMethod 為文本指定輸入法,需要完全限定名(完整的包名)。例如:com.google.android.inputmethod.pinyin,但是這里報(bào)錯(cuò)找不到。
android:inputType 設(shè)置文本的類型,用于幫助輸入法顯示合適的鍵盤(pán)類型。在EditView中再詳細(xì)說(shuō)明,這里無(wú)效果。
android:linksClickable 設(shè)置鏈接是否點(diǎn)擊連接,即使設(shè)置了autoLink。
android:marqueeRepeatLimit 在ellipsize指定marquee的情況下,設(shè)置重復(fù)滾動(dòng)的次數(shù),當(dāng)設(shè)置為marquee_forever時(shí)表示無(wú)限次。
android:ems 設(shè)置TextView的寬度為N個(gè)字符的寬度。這里測(cè)試為一個(gè)漢字字符寬度
android:maxEms 設(shè)置TextView的寬度為最長(zhǎng)為N個(gè)字符的寬度。與ems同時(shí)使用時(shí)覆蓋ems選項(xiàng)。
android:minEms 設(shè)置TextView的寬度為最短為N個(gè)字符的寬度。與ems同時(shí)使用時(shí)覆蓋ems選項(xiàng)。
android:maxLength 限制顯示的文本長(zhǎng)度,超出部分不顯示。
android:lines 設(shè)置文本的行數(shù),設(shè)置兩行就顯示兩行,即使第二行沒(méi)有數(shù)據(jù)。
android:maxLines 設(shè)置文本的最大顯示行數(shù),與width或者layout_width結(jié)合使用,超出部分自動(dòng)換行,超出行數(shù)將不顯示。
android:minLines 設(shè)置文本的最小行數(shù),與lines類似。
android:lineSpacingExtra 設(shè)置行間距。
android:lineSpacingMultiplier 設(shè)置行間距的倍數(shù)。如”1.2”
android:numeric 如果被設(shè)置,該TextView有一個(gè)數(shù)字輸入法。此處無(wú)用,設(shè)置后唯一效果是TextView有點(diǎn)擊效果,此屬性在EdtiView將詳細(xì)說(shuō)明。
android:password 以小點(diǎn)”.”顯示文本
android:phoneNumber 設(shè)置為電話號(hào)碼的輸入方式。
android:privateImeOptions 設(shè)置輸入法選項(xiàng),此處無(wú)用,在EditText將進(jìn)一步討論。
android:scrollHorizontally 設(shè)置文本超出TextView的寬度的情況下,是否出現(xiàn)橫拉條。
android:selectAllOnFocus 如果文本是可選擇的,讓他獲取焦點(diǎn)而不是將光標(biāo)移動(dòng)為文本的開(kāi)始位置或者末尾位置。TextView中設(shè)置后無(wú)效果。
設(shè)置文本陰影:
android:shadowColor 指定文本陰影的顏色,需要與shadowRadius一起使用。
? ? ? ?android:shadowDx 設(shè)置陰影橫向坐標(biāo)開(kāi)始位置。
android:shadowDy 設(shè)置陰影縱向坐標(biāo)開(kāi)始位置。
android:shadowRadius 設(shè)置陰影的半徑。設(shè)置為0.1就變成字體的顏色了,一般設(shè)置為3.0的效果比較好。
android:singleLine 設(shè)置單行顯示。如果和layout_width一起使用,當(dāng)文本不能全部顯示時(shí),后面用“…”來(lái)表示。如android:text="test_ singleLine "
? ? ? ?android:singleLine="true" android:layout_width="20dp"將只顯示“t…”。如果不設(shè)置singleLine或者設(shè)置為false,文本將自動(dòng)換行
android:text設(shè)置顯示文本.
android:textAppearance設(shè)置文字外觀。如“?android:attr/textAppearanceLargeInverse”這里引用的是系統(tǒng)自帶的一個(gè)外觀,?表示系統(tǒng)是否有這種外觀,否則使用默認(rèn)的 外觀。可設(shè)置的值如下:
textAppearanceButton/textAppearanceInverse/textAppearanceLarge/textAppearanceLargeInverse/textAppearanceMedium/textAppearanceMediumInverse/textAppearanceSmal l/textAppearanceSmallInverse
android:textColor 設(shè)置文本顏色
android:textColorHighlight 被選中文字的底色,默認(rèn)為藍(lán)色
android:textColorHint?設(shè)置提示信息文字的顏色,默認(rèn)為灰色。與hint一起使用。
android:textColorLink 文字鏈接的顏色.
android:textScaleX 設(shè)置文字之間間隔,默認(rèn)為1.0f。
android:textSize 設(shè)置文字大小,推薦度量單位”sp”,如”15sp”
android:textStyle設(shè) 置字形[bold(粗體) 0, italic(斜體) 1, bolditalic(又粗又斜) 2] 可以設(shè)置一個(gè)或多個(gè),用“|”隔開(kāi)
android:typeface 設(shè)置文本字體,必須是以下常量值之一:normal 0, sans 1, serif 2, monospace(等寬字體) 3]
android:height 設(shè)置文本區(qū)域的高度,支持度量單位:px(像素)/dp/sp/in/mm(毫米)
android:maxHeight 設(shè)置文本區(qū)域的最大高度
android:minHeight 設(shè)置文本區(qū)域的最小高度
android:width 設(shè)置文本區(qū)域的寬度,支持度量單位:px(像素)/dp/sp/in/mm(毫米),與layout_width的區(qū)別看這里http://blog.csdn.net/tuke_tuke/article/details/50464882?
android:maxWidth 設(shè)置文本區(qū)域的最大寬度
android:minWidth 設(shè)置文本區(qū)域的最小寬度
在設(shè)置TexView屬性時(shí),可以使用Graphical Layout圖形布局的右邊可以看到當(dāng)前android版本API下的所有屬性,如圖
在右邊的屬性欄中還有該組件的父組件的屬性列表,設(shè)置方便。
例子:不同顏色字體帶鏈接的TexView
Main_Activity.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"> <!-- 文本框結(jié)尾處和文本前處繪制圖片 --><TextViewandroid:id="@+id/textView1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:drawableEnd="@drawable/ic_launcher"android:drawableLeft="@drawable/ic_launcher" android:text="我愛(ài)我的祖國(guó)"android:textSize="20dp" /><!-- 設(shè)置中間省略,所有字母大寫(xiě) --><TextViewandroid:id="@+id/textView2"android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="我愛(ài)Contry我愛(ài)contry是嗎是啊啊我愛(ài)Contry我愛(ài)contry是嗎是啊啊"android:ellipsize="middle" android:textAllCaps="true" android:singleLine="true"/><!-- 對(duì)電話,郵件增加鏈接 --><TextViewandroid:id="@+id/textView3"android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="郵件是tuketuke@163.com,電話是15927007602"android:autoLink="email|phone"/><!-- 設(shè)置設(shè)置文字的大小顏色,陰影 --><TextViewandroid:id="@+id/textView4"android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="測(cè)試文字大小顏色陰影"android:textColor="#00f"android:textSize="18dp" android:shadowColor="#00f"android:shadowDx="10.0"android:shadowDy="3.0"android:shadowRadius="3.0"/> <!-- 測(cè)試密碼框--> <TextViewandroid:id="@+id/textView5"android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="郵件是tuketuke@163.com,電話是02725614445"android:password="true"/><!-- 測(cè)試可勾選textview --><CheckedTextViewandroid:id="@+id/textView6"android:layout_width="fill_parent"android:layout_height="wrap_content"android:checkMark="@drawable/ok"android:text="可勾選文本" /></LinearLayout>
例:圓角邊框,漸變背景的TextView
xml文件是:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><!-- 使用xml定義drawable對(duì)象作為背景 --><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/bg_border"android:text="帶邊框的文本"android:textSize="20dp" /><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/bg_border2"android:text="圓角邊框,漸變背景的文本"android:textSize="20dp" /></LinearLayout>
默認(rèn)情況下TexView是不帶邊框的,如果想為T(mén)extView加邊框,可以設(shè)置一個(gè)背景drawable對(duì)象,使用xml定義Drawable對(duì)象
在res/drawable文件夾中可以有直接的drawable資源對(duì)象,如.jpg,.png等圖片資源,也有使用xml定義的drawable對(duì)象,xml 定義的drawable對(duì)象有五種:StateListDrawable(selector根元素),LayerDrawable(layer-list根元素),ShapeDrwable(shape根元素),AnimationDrawable(set根元素),xxx。詳細(xì)的介紹以后會(huì)寫(xiě)文章的
這里的是shape根元素,可以定義形狀背景等,大約有下邊的5個(gè)元素。
bg_border.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" ><!-- 設(shè)置矩形的背景色 --><solid android:color="#0000"></solid><!-- 設(shè)置邊框,寬度和顏色 --><stroke android:width="4px" android:color="#f00"></stroke> </shape> bg_border2.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">"<!-- 設(shè)置矩形的4個(gè)圓角的半徑 --><corners android:topLeftRadius="5px" android:topRightRadius="5px" android:bottomLeftRadius="5px" android:bottomRightRadius="5px"></corners><!-- 設(shè)置邊框,寬度和顏色 --><stroke android:width="4px" android:color="#f0f"></stroke><!-- 設(shè)置漸變色,使用線性類型的漸變色,紅色,綠色,藍(lán)色 --><gradient android:startColor="#f00" android:centerColor="#0f0" android:endColor="#00f" android:type="linear"></gradient>" </shape>
EditView的用法:
EditView的最重要的屬性是inputType屬性,根據(jù)不同的值,調(diào)出不同的輸入鍵盤(pán)。
下面用例子說(shuō)明,例子:用戶友好的輸入界面
xml
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:stretchColumns="1" ><TableRowandroid:id="@+id/tableRow1"android:layout_width="wrap_content"android:layout_height="wrap_content" ><TextViewandroid:id="@+id/textView1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="用戶名:"android:textSize="20dp" /><EditTextandroid:id="@+id/editText1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:ems="10"android:hint="請(qǐng)?zhí)顚?xiě)登錄賬號(hào)"android:selectAllOnFocus="true" ></EditText></TableRow><TableRowandroid:id="@+id/tableRow2"android:layout_width="wrap_content"android:layout_height="wrap_content" ><TextViewandroid:id="@+id/textView2"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="密 碼:"android:textSize="20dp" /><!-- android:inputType="numberPassword"表明只接受數(shù)字密碼 --><EditTextandroid:id="@+id/editText2"android:layout_width="fill_parent"android:layout_height="wrap_content"android:ems="10"android:inputType="numberPassword" /></TableRow><TableRowandroid:id="@+id/tableRow3"android:layout_width="fill_parent"android:layout_height="wrap_content" ><TextViewandroid:id="@+id/textView3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="年 齡:"android:textSize="20dp" /> <!-- android:inputType="number"表明是數(shù)字輸入框 --><EditTextandroid:id="@+id/editText3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:ems="10"android:inputType="number" /></TableRow><TableRowandroid:id="@+id/tableRow4"android:layout_width="wrap_content"android:layout_height="wrap_content" ><TextViewandroid:id="@+id/textView4"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="生 日:"android:textSize="20dp" /> <!-- android:inputType="date"表明是日期輸入框 --><EditTextandroid:id="@+id/editText4"android:layout_width="fill_parent"android:layout_height="wrap_content"android:ems="10"android:inputType="date" /></TableRow><TableRowandroid:id="@+id/tableRow5"android:layout_width="wrap_content"android:layout_height="wrap_content" ><TextViewandroid:id="@+id/textView5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="電話號(hào)碼:"android:textSize="20dp" /> <!-- android:inputType="phone"表明是電話號(hào)碼輸入框 --><EditTextandroid:id="@+id/editText5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:ems="10"android:inputType="phone"android:selectAllOnFocus="true" /></TableRow><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="注冊(cè)" /></TableLayout>
其實(shí)圖形化設(shè)計(jì)的時(shí)候可以直接拖動(dòng)組件就好了,android已經(jīng)定制好了組件
總結(jié)
以上是生活随笔為你收集整理的UI组件之TextView及其子类(一)TextView和EditText的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: UI组件之TextView及其子类
- 下一篇: UI组件之TextView及其子类(二)