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

Android View 基礎筆記:TextView、樣式與超連結

編輯:關於 Android 程式設計

在 Android 中,幾乎所有可見元件都與 View 有關。View 是介面的基礎類別,而 TextView 則是最常用的文字顯示元件之一。

View 的基本概念

大多數 Android 元件都繼承自 View。開發者可以透過程式碼設定屬性,也可以透過 XML 版面配置檔進行設定。兩種方式各有用途,通常會依需求混用。

TextView 常用設定

TextView 主要用於顯示文字,可設定文字內容、字級、顏色、樣式與行距等屬性。若需要讓文字可點擊,可以設定 autoLink,或透過程式碼套用 HTML 與 LinkMovementMethod。

String html = "聯絡我";
CharSequence text = Html.fromHtml(html);
textView.setText(text);
textView.setMovementMethod(LinkMovementMethod.getInstance());

這樣設定後,TextView 中的網址或連結就能被辨識並觸發對應動作。

樣式表的使用

若多個元件需要相同外觀,可使用樣式表集中管理。樣式通常定義在 res/values/styles.xml 中,透過 name 指定樣式名稱,並在 item 中設定文字大小、顏色、粗體等屬性。

<resources>
    <style name="msg_style">
        <item name="android:textSize">45dp</item>
        <item name="android:textColor">#00FF00</item>
        <item name="android:textStyle">bold|italic</item>
    </style>
</resources>

版面配置中即可透過 style 屬性引用該樣式,讓多個元件保持一致外觀,也方便後續維護。

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