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

Android 教學:TextView 文字顏色設定的多種方法總整理

編輯:關於 Android 程式設計

本文透過實例,整理了 Android 開發中設定 TextView 文字顏色的幾種常見做法,分享給大家參考。

先談談 setTextColor(int a) 這個方法,其中的參數 a 是傳入的顏色值。例如紅色 0xff0000,若直接傳入 R.color.red 是無法成功設定顏色的,必須透過本文中介紹的第三種方法,先取得資源的顏色值再傳入才行。
tv.setTextColor(this.getResources().getColor(R.color.red));

TextView 的文字顏色設定大致可分為以下兩種途徑:

1、直接透過設定檔(XML)設定

2、在 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://www.jb51.net/</string> <string name="app_name">本站</string> </resources> 

上面將資源拆分成三個部分,目的是為了讓結構更清晰;當然,你也可以只建立一個 XML 檔放在 res 目錄下,而且檔案名稱可以隨意命名。

這裡有兩個地方需要特別注意:

1、main.xml 的 TextView 標籤中:android:textColor="@color/red"
2、color.xml 中:<color name="red">#FF0000</color>

其中 @color 指的是取得資源檔案(所有 res 目錄下的 XML 檔)中的 <color> 標籤,而 /red 則是在該標籤下尋找 name 值為 red 的內容,此時其值為 #FF0000。

因此,這裡其實也可以改寫成:android:textColor="@drawable/red",其中 @drawable 指的是取得資源檔案中的 <drawable> 標籤,/red 同樣是在該標籤下尋找 name 值為 red 的內容。以此類推,相信你也能推想出在 strings.xml 中該如何處理了。

接著來看第二種方式:在 Activity 類別中設定。

1、先將 main.xml 修改成以下樣子,也就是移除 android:textColor="@color/red":

 <?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> 

2、修改 Activity 的 onCreate 方法,原始程式碼如下:

 package yahaitt.study03_01; import android.app.Activity; import android.os.Bundle; public class Study03_01 extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } 

第一步:取得 TextView 控制項,命名為 tv。

第二步:透過 TextView 的 setTextColor 方法設定文字顏色,這裡共有三種方式:

第 1 種:tv.setTextColor(android.graphics.Color.RED);——使用系統自帶的顏色類別。

第 2 種:tv.setTextColor(0xffff00ff);——其中 0xffff00ff 是 int 型別的資料,可拆分為 0x | ff | ff00ff,0x 代表顏色整數的標記,ff 表示透明度,ff00ff 表示顏色。特別注意:此處必須使用 8 位元的顏色表示,不接受 ff00ff 這種 6 位元的寫法。

第 3 種:tv.setTextColor(this.getResources().getColor(R.color.red));——透過取得資源檔來設定。依不同情況,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); } } 

希望本文所述能對大家的 Android 程式設計有所幫助。

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