Android UI 控件之TextView
TextView(文本框),用於顯示文本的一個控件
基本屬性
android:id 設置該控件的唯一標識;
android:layout_width 設置控件的寬度,可設置warp_content(自適應寬度)match_parent or fill_parent(充滿父組件)或者寬度值(數值加寬度單位);
android:background 設置控件的背景;
android:layout_weight 權重,控件佔父控件剩餘空間的比例大小,可設置0,1,2...,未設置則默認爲0;
android:gravity 設置元素在該控件裏的顯示位置,top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical等,多個可用|分割;
android:layout_gravity 設置控件在父控件中的顯示位置,top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical等,多個可用|分割。
android:layout_margin 設置控件距離上下左右的距離;
android:layout_marginTop 設置控件距離上面的距離;
android:layout_marginLeft 設置控件距離左邊距離;
android:layout_marginRight 設置控件距離右邊距離;
android:layout_marginBottom 設置控件距離下面距離;
android:layout_marginStart 設置控件距離開始位置的距離;
android:layout_marginEnd 設置控件距離結束位置的距離;
android:padding 設置控件上下左右的內邊距;
android:paddingLeft 設置控件距離左邊的內邊距;
android:paddingTop 設置控件距離上面的內邊距;
android:paddingRight 設置控件距離右邊的內邊距;
android:paddingBottom 設置控件距離底部的內邊距;
android:paddingStart 設置控件距離開始位置的內邊距;
android:paddingEnd 設置控件距離結束位置的內邊距;
android:maxHeight 設置控件的最大高度;
android:maxWidth 設置控件的最大寬度;
android:minHeight 設置控件的最小高度;
android:minWidth 設置控件的最小寬度;
android:drawablePadding 設置控件中圖片與文字之間的空隙;
android:drawableLeft 設置控件圖片顯示在文字左邊;
android:drawableRight 設置控件圖片顯示在文字右邊;
android:drawableTop 這是控件圖片顯示在文字上面;
android:drawableBottom 設置控件圖片顯示在文字下面;
android:drawableStart 設置控件圖片顯示在文字的開始處;
android:drawableEnd 設置控件的圖片顯示在文字的結尾處;
android:text 設置控件顯示的文字;
android:textColor 設置控件顯示的文字顏色;
android:textSize 設置控件顯示的文字大小,推薦使用sp;
android:textAllCaps 設置控件文本全部大寫。可能的值是“true”或“false”;
android:textStyle 設置字形[bold(粗體) 0, italic(斜體) 1,bolditalic(又粗又斜) 2] 可以設置一個或多個,用“|”隔開;
android:typeface 設置文本字體,必須是以下常量值之一:normal 0, sans 1, serif 2;
android:textAppearance 設置文字外觀。如“?android:attr/textAppearanceLargeInverse
”這裏引用的是系統自帶的一個外觀,?表示系統是否有這種外觀,否則使用默認的外觀。可設置的值如下:textAppearanceButton/textAppearanceInverse/textAppearanceLarge/textAppearanceLargeInverse/textAppearanceMedium/textAppearanceMediumInverse/textAppearanceSmall/textAppearanceSmallInverse;
android:textColorHighlight 設置控件被選中文字的底色,默認爲藍色;
android:textColorHint 設置提示信息文字的顏色,默認爲灰色。與hint一起使用;
android:textColorLink 設置文字鏈接的顏色;
android:textScaleX 設置文字之間間隔,默認爲1.0f;
android:textIsSelectable 表示可被選擇的非可編輯的文本的內容。可能的值是“true”或“false”。;
android:textSelectHandle; 設置系統文本選中中部時出現的複製箭頭;
android:textSelectHandleLeft; 設置系統文本選中左邊時出現的複製箭頭;
android:textSelectHandleRight; 設置系統文本選中右邊時出現的複製箭頭;
android:maxLength 設置文本的最大長度;
android:maxEms 設置最大系統兼容;
android:maxLines 設置最大文本行數;
android:minEms 設置最小系統兼容;
android:minLines 設置最小文本行數;
android:shadowColor 設置陰影顏色;
android:shadowDx 設置陰影水平偏移;
android:shadowDy 設置陰影垂直偏移;
android:shadowRadius 設置陰影半徑;
android:singleLine 設置是否單行顯示;
android:lines 設置多行顯示;
android:lineSpacingExtra 設置額外的文字間距;
android:lineSpacingMultiplier 設置額外的文字間距,乘數;
android:autoLink 設置識別鏈接類型,當文字中出現了URL,E-Mail,電話號碼,地圖的時候,我們可以通過設置autoLink屬性;當我們點擊文字中對應部分的文字,即可跳轉至某默認APP,比如一串號碼,點擊後跳轉至撥號界面!可設置all,web,email,phone,map,none;
android:autoText 設置是否自動檢測錯誤;
android:bufferType 決定getText()返回的類型;
android:capitalize 設置指定使用類型,指定該TextView中有一個文本輸入法會自動利用什麼類型的用戶。不要自動大寫任何東西 none,大寫每句的第一個字 sentences,大寫每個單詞的第一個字母 words,大寫每一個字符 characters;
android:digits 設置文字輸入規則;
android:cursorVisible 設置光標可見(默認值)或不可見。默認爲false;
android:editable 如果設置爲true,指定TextView的一個輸入法;
android:fontFamily 設置字體系列(由字符串命名)的文本;
android:gravity 指定如何排列由視圖的x和/或y軸的文本時,該文本比視圖小;
android:hint 設置文本空白時的提示語;
android:inputType 數據的類型被放置在一個文本字段。手機,日期,時間,號碼,密碼等;
android:password 字段的字符是否顯示爲密碼的點,而不是它們本身。可能的值是“true”或“false”;
android:phoneNumber 如果設置,則指定TextView具有一個電話號碼的輸入法。可能的值是“true”或“false”;
android:marqueeRepeatLimit 設置表示滾動回數,marquee_forever表示一直滾動;
android:ellipsize 設置文字超出範圍顯示方式,start省略號在開、middle省略號在中間、end省略號在結尾、marquee跑馬燈顯示;
focusableInTouchMode="true";
android:focusable="true" 要顯示該跑馬燈,view必須要獲得焦點,只有在取得焦點的情況下跑馬燈纔會顯示;
一些屬性講解
layout_weight
下面給出設置layout_weight時控件寬度的計算:
如下代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textColor="@android:color/white"
android:layout_weight="1"
android:gravity="center"
android:background="#FF00FF"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:gravity="center"
android:background="#00FF00"
android:textColor="@android:color/white"
android:layout_weight="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:gravity="center"
android:background="#0000FF"
android:textColor="@android:color/white"
android:layout_weight="3" />
</LinearLayout>
三個文本框的都是layout_width=”wrap_content”時,得到以下效果:
系統先給3個TextView分配他們的寬度值 wrap_content(寬度足以包含他們的內容1,2,3即可),然後會把剩下來的屏幕空間按照1:2:3的比列分配給3個textview,所以就出現了上面的圖像。
而當layout_width=”match_parent”時,如果分別給三個TextView設置他們的Layout_weight爲1、2、2的話即代碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1"
android:textColor="@android:color/white"
android:layout_weight="1"
android:gravity="center"
android:background="#FF00FF"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2"
android:gravity="center"
android:background="#00FF00"
android:textColor="@android:color/white"
android:layout_weight="2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3"
android:gravity="center"
android:background="#0000FF"
android:textColor="@android:color/white"
android:layout_weight="2" />
</LinearLayout>
則會出現如下效果:
系統先給3個textview分配他們所要的寬度match_parent,也就是說每一都是填滿他的父控件,這裏就是屏幕的寬度,那麼這時候的剩餘空間=1個parent_width-3個parent_width=-2個parent_width (parent_width指的是屏幕寬度 )
那麼第一個TextView的實際所佔寬度應該=match_parent的寬度,即parent_width + 他所佔剩餘空間的權重比列1/5 * 剩餘空間大小(-2 parent_width)=3/5parent_width
同理第二個TextView的實際所佔寬度=parent_width + 2/5*(-2parent_width)=1/5parent_width;
第三個TextView的實際所佔寬度=parent_width + 2/5*(-2parent_width)=1/5parent_width;所以就是3:1:1的比列顯示了。
paddingStart與paddingEnd
根據左右對齊方式來起作用的,左對齊時, paddingStart 對應 paddingLeft, paddingEnd 對應 paddingRight右對齊時, 正好相反 。
gravity與layout_gravity
gravity 設置控件裏面的元素顯示的位置,可設top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical等,多個可用|分割;
layout_gravity 設置控件在父控件的顯示位置,top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical等,多個可用|分割。
以下爲gravity與layout_gravity的效果:
TextView 中設置gravity,這TextView中文字位置顯示爲gravity設置的值:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="this is word."
android:background="#FF00FF"
android:gravity="center" />
</LinearLayout>
效果圖如下:
TextView 中設置layout_gravity,TextView在父控件中顯示的位置如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is word."
android:background="#FF00FF"
android:layout_gravity="center"/>
</LinearLayout>
效果圖如下:
實際例子
帶邊框的TextView
編寫一個ShapeDrawable資源文件,首先在drawable目錄下新建一個xml文件,然後寫入如下代碼:(demo.xml)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 設置背景顏色色 -->
<solid android:color="#FFFFFF" />
<!-- 設置邊框顏色與邊框寬度 -->
<stroke
android:width="2dp"
android:color="#51A8F2" />
<!-- 設置四個圓角的半徑 如果四個角圓角半徑一樣可以直接設置radius="10dp"-->
<corners
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
<!-- 設置邊距-->
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>
然後在TextView中設置background爲上面的ShapeDrawable資源文件,如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:text="this is word."
android:gravity="center"
android:background="@drawable/demo"
android:layout_gravity="center"/>
</LinearLayout>
效果圖如下:
ShapeDrawble 資源文件的節點和屬性介紹:
<solid android:color = "xxx"> 這個是設置背景顏色的
<stroke android:width = "xdp" android:color="xxx"> 這個是設置邊框的粗細,以及邊框顏色的
<padding androidLbottom = "xdp"> 這個是設置邊距的
<corners android:topLeftRadius="10dp"> 這個是設置圓角的
<gradient> 這個是設置漸變色的,可選屬性有: startColor:起始顏色 endColor:結束顏色 centerColor:中間顏色 angle:方向角度,等於0時,從左到右,然後逆時針方向轉,當angle = 90度時從下往上 type:設置漸變的類型
<size android:height="100dp" android:width="100dp"></size>設置ShapeDrawable的大小
帶圖片的TextView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FFFFFF">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:drawableTop="@mipmap/loading_more_earth0"
android:drawableLeft="@mipmap/loading_more_earth2"
android:drawableRight="@mipmap/loading_more_earth4"
android:drawableBottom="@mipmap/loading_more_earth6"
android:drawablePadding="10dp"
android:text="帶圖片的TextView" />
</LinearLayout>
顯示效果圖如下:
在xml中我們無法設置圖片的大小,如果想設置圖片的大小,必須通過Java代碼實現,如下:
TextView textView = (TextView) findViewById(R.id.textView);
Drawable[] drawable = textView.getCompoundDrawables();
// 數組下表0~3,依次是:左上右下
drawable[1].setBounds(100, 0, 200, 200);
textView.setCompoundDrawables(drawable[0],drawable[1],drawable[2],drawable[3]);
代碼解析:
- Drawable[] drawable = textView.getCompoundDrawables( ); 獲得四個不同方向上的圖片資源,數組元素依次是:左上右下的圖片
- drawable[1].setBounds(100, 0, 200, 200); 接着獲得資源後,可以調用setBounds設置左上右下座標點,比如這裏設置了代表的是: 長是:從離文字最左邊開始100dp處到200dp處 寬是:從文字上方0dp處往上延伸200dp!
- textView.setCompoundDrawables(drawable[0], drawable[1], drawable[2], drawable[3]);爲TextView重新設置drawable數組!沒有圖片可以用null代替!
顯示Html
除了顯示普通文本外,TextView還預定義了一些類似於HTML的標籤,通過這些標籤,我們可以使 TextView顯示不同的字體顏色,大小,字體,甚至是顯示圖片,或者鏈接等!我們只要使用HTML中的一些 標籤,加上android.text.HTML類的支持,即可完成上述功能!
當然,並不是支持所有的標籤,常用的有下述這些:
<font>:設置顏色和字體。
<big>:設置字體大號
<small>:設置字體小號
<i><b>:斜體粗體
<a>:連接網址
<img>:圖片
如果直接setText的話是沒作用的,我們需要調用Html.fromHtml()方法將字符串轉換爲CharSequence接口, 然後再進行設置,如果我們需要相應設置,需要爲TextView進行設置,調用下述方法:setMovementMethod(LinkMovementMethod.getInstance())
TextView textview = (TextView)findViewById(R.id.tv);
String s = "<font color='blue'><b>百度一下,你就知道~:</b></font><br>";
s += "<a href = 'http://www.baidu.com'>百度</a>";
textview.setText(Html.fromHtml(s));
textview.setMovementMethod(LinkMovementMethod.getInstance());
SpannableString&SpannableStringBuilder定製文本
SpannableString、SpannableStringBuilder與String的關係
首先SpannableString、SpannableStringBuilder基本上與String差不多,也是用來存儲字符串,但它們倆的特殊就在於有一個SetSpan()函數,能給這些存儲的String添加各種格式或者稱樣式(Span),將原來的String以不同的樣式顯示出來,比如在原來String上加下劃線、加背景色、改變字體顏色、用圖片把指定的文字給替換掉,等等。所以,總而言之,SpannableString、SpannableStringBuilder與String一樣, 首先也是傳字符串,但SpannableString、SpannableStringBuilder可以對這些字符串添加額外的樣式信息,但String則不行。
注意:如果這些額外信息能被所用的方式支持,比如將SpannableString傳給TextView;也有對這些額外信息不支持的,比如Canvas繪製文字,對於不支持的情況,SpannableString和SpannableStringBuilder就是退化爲String類型,直接顯示原來的String字符串,而不會再顯示這些附加的額外信息。
SpannableString與SpannableStringBuilder區別
它們的區別在於 SpannableString像一個String一樣,構造對象的時候傳入一個String,之後再無法更改String的內容,也無法拼接多個 SpannableString;而SpannableStringBuilder則更像是StringBuilder,它可以通過其append()方法來拼接多個String。
//使用SpannableString,必須一次傳入,構造完成
SpannableString spannableString = new SpannableString("hyn_獨行者");
//使用SpannableStringBuilder,可以使用append()再添加
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
spannableStringBuilder.append("hyn_獨行者");
spannableStringBuilder.append("就愛");
spannableStringBuilder.append("獨行");
可供我們使用的API
- BackgroundColorSpan 背景色
- ClickableSpan 文本可點擊,有點擊事件
- ForegroundColorSpan 文本顏色(前景色)
- MaskFilterSpan 修飾效果,如模糊(BlurMaskFilter)、浮雕(EmbossMaskFilter
- MetricAffectingSpan 父類,一般不用
- RasterizerSpan 光柵效果
- StrikethroughSpan 刪除線(中劃線)
- SuggestionSpan 相當於佔位符
- UnderlineSpan 下劃線
- AbsoluteSizeSpan 絕對大小(文本字體)
- DynamicDrawableSpan 設置圖片,基於文本基線或底部對齊。
- ImageSpan 圖片
- RelativeSizeSpan 相對大小(文本字體)
- ReplacementSpan 父類,一般不用
- ScaleXSpan 基於x軸縮放
- StyleSpan 字體樣式:粗體、斜體等
- SubscriptSpan 下標(數學公式會用到)
- SuperscriptSpan 上標(數學公式會用到)
- TextAppearanceSpan 文本外貌(包括字體、大小、樣式和顏色)
- TypefaceSpan 文本字體
- URLSpan 文本超鏈接
使用示例
SpannableString span = new SpannableString("紅色打電話斜體刪除線綠色下劃線圖片:.");
//1.設置背景色,setSpan時需要指定的flag,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE(前後都不包括)
span.setSpan(new ForegroundColorSpan(Color.RED), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//2.用超鏈接標記文本
span.setSpan(new URLSpan("tel:4155551212"), 2, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//3.用樣式標記文本(斜體)
span.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 5, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//4.用刪除線標記文本
span.setSpan(new StrikethroughSpan(), 7, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//5.用下劃線標記文本
span.setSpan(new UnderlineSpan(), 10, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//6.用顏色標記
span.setSpan(new ForegroundColorSpan(Color.GREEN), 10, 13,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//7.獲取Drawable資源
Drawable d = getResources().getDrawable(R.drawable.icon);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
//8.創建ImageSpan,然後用ImageSpan來替換文本
ImageSpan imgspan = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
span.setSpan(imgspan, 18, 19, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);