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

Android TextView 加入下劃線的方法

編輯:關於 Android 程式設計

在 Android 中,若需要讓 TextView 的文字帶有下劃線,可透過資源字串、HTML 轉換或 Paint 設定來完成。

在資源檔中使用 HTML 標籤

可在 strings.xml 直接放入 u 標籤:

<resources>
    <string name="hello"><u>phone:0123456</u></string>
    <string name="app_name">MyLink</string>
</resources>

在程式碼中使用 Html.fromHtml

TextView textView = (TextView) findViewById(R.id.tv_test);
textView.setText(Html.fromHtml("<u>" + "0123456" + "</u>"));

使用 Paint 旗標

也可以直接對 TextView 的 Paint 設定下劃線旗標:

tvTest.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
tvTest.getPaint().setAntiAlias(true);

這三種方式各有適用情境:資源檔適合固定文字,Html.fromHtml 適合動態組字串,Paint 則適合直接控制繪製樣式。

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