Android UI系列:TextView顯示文字
Android下有眾多的Widget,而TextView可以說是最基礎的,或者說是最基本的的控制元件。和其它程式語言相同的label標籤一樣,一般用來顯示固定長度的文字字串。
一、例項
在介紹前先演示一下例項
效果圖一如上
佈局檔案text_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/textView_content" /> </LinearLayout>
java程式碼TextViewActivity.java
package com.example.test; import android.app.Activity; import android.os.Bundle; public class TextViewActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.text_view); } }
再在AndroidManifest.xml配置一下<activity android:name="TextViewActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
其中用的字串資源是
<string name="textView_content">我是Textview,大家要好好學習!</string>
四、能過程式碼方式建立TextView
TextView可以通過在佈局檔案進行設定,也可以通過程式碼生成
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView textView= new TextView(this); textView.setText(getText(R.string.textView_content)); setContentView(textView); }
執行的效果和圖一完全一樣五、走馬燈效果
android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:marqueeRepeatLimit="marquee_forever" android:singleLine="true"
加上以上屬性就可以實現走馬燈的效果了注意:
1.要單行,如果沒有設定單行,多行可以全部顯示出來,是沒有走馬燈的效果的
2.設定為焦點
3沒有滾動球的裝置 android:focusableInTouchMode="true"屬性是必須的
4.如果內容一行就可以顯示出來的,請重複一次內容,如果重複後還是可以一行顯示的,請再重複,如
<string name="textView_content">我是Textview,大家要好好學習!我是Textview,大家要好好學習!</string>
閃爍效果
別人的走馬燈效果要閃爍一下,那我們也來實現
public class TextViewActivity extends Activity { private TextView textView = null; private boolean changeColor = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.text_view); textView = (TextView) findViewById(R.id.text_view); new Thread(new MyThread()).start(); } //實現半秒就改變一次顏色 class MyThread implements Runnable { @Override public void run() { //傳說中死迴圈,半秒就改變一次 while (true) { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } Message message = new Message(); if (changeColor = !changeColor) { message.what = COLOR_BLACK; } else { message.what = COLOR_RED; } handler.sendMessage(message); } }// end run } private final int COLOR_RED = 1;//紅色 private final int COLOR_BLACK = 2;//黑色 private MyHandler handler = new MyHandler(); class MyHandler extends Handler { @Override public void handleMessage(Message msg) { switch (msg.what) { case COLOR_RED: textView.setTextColor(Color.RED); break; case COLOR_BLACK: textView.setTextColor(Color.BLACK); break; default: break; } } } }
以上的效果是半秒就改變一下字型的顏色,紅黑不停地切換
六、在文字中建立上下文連結
如果TextView中的文字字串包含有網址、郵箱、電話號碼或地址,是否可以點選進行相應的跳轉到我們要去的頁面,如字串的網址連結,跳到相應的網頁,那樣我們就可以直接檢視內容了,這不是一件很美妙的事嗎?
Must be one or more (separated by '|') of the following constant values.
Constant Value Description none
0x00 都不匹配 (default). web
0x01 匹配網址連結 0x02 匹配email地址 phone
0x04 匹配電話號碼 map
0x08 匹配地圖地址. all
0x0f 匹配所有(equivalent to web|email|phone|map). 例如xml設定
android:autoLink="web"
android:autoLink="web|email"
android:autoLink="all"
java程式碼設定
textView.setAutoLinkMask(Linkify.EMAIL_ADDRESSES);
textView.setAutoLinkMask(Linkify.EMAIL_ADDRESSES|Linkify.WEB_URLS);
textView.setAutoLinkMask(Linkify.ALL);
七、獲取本地資源及格式化
方法1
public final CharSequence getText (int resId)
Return a localized, styled CharSequence from the application's package's default string table.方法2
public final String getString (int resId)
Return a localized string from the application's package's default string table.方法3
getResources().getString(int resId)
Return the string value associated with a particular resource ID. It will be stripped of any styled text information.演示:
<string name="text_format">Plain, <b>bold</b>, <i>italic</i>, <b><i>bold-italic</i></b></string>
text_view_1.setText(getText(R.string.text_format)); text_view_2.setText(getString(R.string.text_format)); text_view_3.setText(getResources().getString(R.string.text_format));
TextView 也可以支援html格式顯示
主要是使用Html.fromHtml(String str)方法
String htmlStr="我是<font color=red>中國人</font>。<br/>我你他也是<font color=green>中國人</font>呀。"; text_view_4.setText(Html.fromHtml(htmlStr));
string.xml特殊字元string.xml中有特殊的字元,會報錯的,那應該怎樣解決呢
一個方法是用轉義符,還有一個更簡單的方法就是用<![CDATA[]]>形式
<string name="other _format"><![CDATA[在這裡輸入特殊的字元,不會報錯的]]></string>
string.xml字串中帶引數<string name="textview_param">我是%1$s,我的年齡是%2$d</string> text_view_5.setText(getString(R.string.textview_param, "科學家",20));
結果顯示:我是科學家,我的年齡是%1$s 1表示第一個引數,s表示為字串
%2$d 2表示第二個引數,d表示為數字,整數、小數都可以
八、顯示圖片
上圖是android桌面上的應用圖示,應用名稱上面是應用logo,這個怎樣的實現的
是不是用了兩個控制元件,一個是ImageView,另一個是TextView
<ImageView />
<TextView />其實不是,真正實現只是用了一個TextView
<TextView android:drawableTop="@drawable/ic_launcher"/>
android:drawableTop表示是textView上面顯示圖片
android:drawableBottom表示底部顯示圖片
android:drawableLeft表示左邊顯示圖片
android:drawableRight表示右邊顯示圖片
九、屬性介紹
android:ellipsize
內容過長時,可以加省略號
android:ellipsize = "none" 預設值,沒有省略號
android:ellipsize = "end" 省略號在結尾
android:ellipsize = "start" 省略號在開頭
android:ellipsize = "middle" 省略號在中間
android:ellipsize = "marquee" 跑馬燈注意要加行數限制,要不省略號不起作用
如 android:maxLines="2" 超過兩行將出現省略號
或者android:singleLine="true"限制為1行