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

Android TextView 文字顏色設置方式完整整理

編輯:關於 Android 程式設計

先釐清一個常見困惑:setTextColor(int color) 傳入的是「顏色實際值」,例如紅色就是 0xff0000。若直接寫 R.color.red,傳進去的只是一個資源 ID 整數,並不會得到紅色——必須先透過 getResources().getColor() 取出真正的色值再傳入:

 tv.setTextColor(this.getResources().getColor(R.color.red));

整體來說,TextView 的文字顏色有兩條路:透過 XML 設定檔,或在 Activity 中用程式碼設定。

方式一:在配置檔中設定

適合靜態或初始顏色,寫法如下:

main.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/white" > <TextView android:id="@+id/tv01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:autoLink="all" android:textColor="@color/red" /> </LinearLayout> 

color.xml

 <?xml version="1.0" encoding="utf-8"?> <resources> <drawable name="white">#FFFFFF</drawable> <drawable name="dark">#000000</drawable> <drawable name="red">#FF0000</drawable> </resources> 

strings.xml

 <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">地址:https://yahaitt.javaeye.com</string> <string name="app_name">丫梨的筆記本</string> </resources> 

上面刻意把資源拆成三個檔案,純粹是為了清楚;實際上你也可以只建一個 XML 放在 res 目錄下,檔名自訂即可。

這裡有兩個關鍵點:

1. main.xml 中寫 android:textColor="@color/red"

2. color.xml 中定義 <color name="red">#FF0000</color>

@color 表示從資源檔(res 目錄下所有 XML)中取 <color> 標籤,/red 則是找出 name 為 red 的那一項,其值為 #FF0000。依此類推,寫成 @drawable/red 就是取 <drawable> 標籤下的 red,字串資源也是同樣道理。

方式二:在 Activity 中設定

先把 main.xml 中的 android:textColor 屬性移除:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/white" > <TextView android:id="@+id/tv01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:autoLink="all" /> </LinearLayout> 

接著在 onCreate 中取得 TextView 並設定顏色,共三種寫法:

第 1 種:tv.setTextColor(android.graphics.Color.RED); 使用系統內建的顏色類。

第 2 種:tv.setTextColor(0xffff00ff); 直接給 int 值。可以拆解成 0x | ff | ff00ff 來理解:0x 是顏色整數的標記,接著兩位 ff 是透明度(alpha),最後六位是 RGB。要注意這裡必須是完整的 8 位寫法,只寫 ff00ff 這種 6 位是行不通的。

第 3 種:tv.setTextColor(this.getResources().getColor(R.color.red)); 透過資源檔取值。依情況也可以是 R.string.red 或 R.drawable.red,前提是對應的資源文件中已有相應定義:

 <color name="red">#FF0000</color> <drawable name="red">#FF0000</drawable> <string name="red">#FF0000</string> 

完整程式碼:

 package yahaitt.study03_01; import android.app.Activity; import android.content.res.Resources; import android.graphics.Color; import android.os.Bundle; import android.widget.TextView; public class Study03_01 extends Activity { private TextView tv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView)this.findViewById(R.id.tv01); //tv.setTextColor(Color.RED); //tv.setTextColor(0xff000000); } } 

實務建議:靜態顏色優先放在 XML,方便設計與主題維護;需要依狀態動態切換時再用程式碼處理,避免把色值硬寫在 Java 裡。

延伸閱讀可參考本站《Android 開發入門與進階教程》《Android 通信方式總結》《Android 基本組件用法總結》《Android 視圖 View 技巧總結》《Android 布局 layout 技巧總結》與《Android 控件用法總結》等專題。

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