Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

TextView 使用大全:12 個實務技巧一次整理

編輯:關於 Android 開發

TextView 使用大全:12 個實務技巧一次整理

先前計畫寫一系列 Android 初級開發教學,結合實際工作經驗整理常用技術,讓入門者少走一些彎路。系列文章的好處是學起來有頭有尾,不會東一塊西一塊。而這一系列,就從我們每天都會用到的 TextView 開始。

1. 設定背景顏色

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FF00FF"
    android:layout_marginTop="10dp"
    android:text="設定背景顏色" />

2. 程式碼中動態賦值

可以直接傳字串,也可以傳字串資源 ID:

TextView tv0 = (TextView) findViewById(R.id.tv0);
tv0.setText("如何在程式裡面動態賦值");

3. 多字串動態組裝

先在 strings.xml 中定義帶占位符的字串:

<string name="testing">這是一個數:%1$d, 這是兩位數:%2$d,這是三位數:%3$s</string>

再在 Java 中傳入對應參數:

tv1.setText(getString(R.string.testing, new Integer[]{11, 21, 31}));

4. 顯示 HTML 內容

字體顏色改為紅色,可用 Html.fromHtml() 解析。注意它並不支援 HTML 標籤上的 style 屬性:

String html = "<font color='red'>TextVie 顯示 html 字體顏色為紅色</font><br/>";
tv3.setText(Html.fromHtml(html));

5. 設定點擊事件

點擊事件定義在父類 View 上,所以所有 Android 元件都有這個能力。這裡用匿名內部類的寫法:

tv4.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(MainActivity.this, "點擊了 TextView4", Toast.LENGTH_LONG).show();
    }
});

6. 文字陰影與加粗

陰影效果需要四個參數:

  • android:shadowColor:陰影顏色
  • android:shadowDx:水平方向偏移量
  • android:shadowDy:垂直方向偏移量
  • android:shadowRadius:陰影模糊半徑
<TextView
    android:id="@+id/tv5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:shadowColor="#ff000000"
    android:shadowDx="10"
    android:shadowDy="10"
    android:shadowRadius="1"
    android:text="文字陰影,文字加粗" />

7. 文字搭配圖片

四個方向各自對應一個屬性,另外可用 drawablePadding 控制間距:

  • drawableBottom:在文字底端繪製圖像
  • drawableLeft:在文字左側繪製圖像
  • drawableRight:在文字右側繪製圖像
  • drawableTop:在文字頂端繪製圖像
<TextView
    android:id="@+id/tv6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:drawableLeft="@drawable/ic_launcher"
    android:drawablePadding="10dp"
    android:gravity="center_vertical"
    android:text="文字+圖片" />

8. Spannable 樣式類

只替部分字元套上樣式,是 SpannableString 的拿手戲。以下把前 10 個字元加上紅色背景:

SpannableString spannableString = new SpannableString("TextView 的樣式類 Span 使用詳解");
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.RED);
spannableString.setSpan(backgroundColorSpan, 0, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv7.setText(spannableString);

9. 局部可點擊文字

重點:使用 ClickableSpan 時,必須設定 MovementMethod,否則點擊不會有任何反應。

SpannableString spannableClickString = new SpannableString("TextView 設定點擊事件 Span");
ClickableSpan clickableSpan = new ClickableSpan() {
    @Override
    public void onClick(View widget) {
        Toast.makeText(MainActivity.this, "TextView 設定點擊事件 Span", Toast.LENGTH_LONG).show();
    }
};
spannableClickString.setSpan(clickableSpan, 11, 15, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
tv8.setMovementMethod(LinkMovementMethod.getInstance());
tv8.setText(spannableClickString);

10. 點擊時切換背景

第一步,在 drawable 目錄建立 selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:drawable="@color/textview_click_background" android:state_focused="true"/>
    <item android:drawable="@color/textview_click_background" android:state_pressed="true"/>
    <item android:drawable="@color/textview_default"/>
</selector>

第二步,在佈局中套用背景:

android:background="@drawable/selector_textview"

第三步,加上點擊事件——一定要掛監聽,否則背景不會變:

findViewById(R.id.tv9).setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(MainActivity.this, "點擊了 TextView9", Toast.LENGTH_LONG).show();
    }
});

11. 上下左右邊框

用 layer-list 疊兩層 shape,底層當邊框色、上層內縮當填充色:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="https://schemas.android.com/apk/res/android" >
    <!-- 邊框顏色 -->
    <item>
        <shape>
            <solid android:color="#00FF00"/>
        </shape>
    </item>
    <!-- 上、左、右各留 8dp 作為邊框 -->
    <item android:top="8dp" android:left="8dp" android:right="8dp" >
        <shape>
            <!-- 內容填充顏色 -->
            <solid android:color="#FFFFFF" />
        </shape>
    </item>
</layer-list>

12. 圓角與邊框

改用 shape 即可同時處理背景色、邊框與圓角:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android" >
    <!-- 預設背景色 -->
    <solid android:color="#FF00FF"/>
    <!-- 邊框 -->
    <stroke android:width="1dp" android:color="@android:color/black" />
    <!-- 圓角弧度 -->
    <corners android:radius="20dp"/>
</shape>

最終效果如下:

延伸建議

一個實務上的經驗:如果 TextView 能達到的效果,就不要動不動換成 Button。TextView 的樣式控制更細緻、也更靈活,尤其在需要自訂邊框、圓角、圖文混排時,往往比改寫 Button 的預設樣式省事得多。

其他值得延伸閱讀的主題:TextView 陰影效果、TextView 進階特性、TextView 屬性大全,以及 Span 樣式類的深入用法。

以上就是日常最常用到的 TextView 知識整理,之後若有新發現再回來補充。有問題也歡迎留言討論。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved