Android編程實現TextView字型顏色設定的方法小結_Android

來源:互聯網
上載者:User

本文執行個體講述了Android編程實現TextView字型顏色設定的方法。分享給大家供大家參考,具體如下:

對於setTextView(int a)這裡的a是傳進去顏色的值。例如,紅色0xff0000是指0xff0000如何直接傳入R.color.red是沒有辦法設定顏色的,只有通過文章中的第三種方法先拿到資源的顏色值再傳進去。

複製代碼 代碼如下:
tv.setTextColor(this.getResources().getColor(R.color.red));

關鍵字: android textview color

TextView的字型設定方法:

1、直接通過設定檔設定

2、在Activity類中進行設定

第一種方式很簡單,用於靜態或初始文字顏色的設定,方法如下:

main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://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">地址:http://www.jb51.net/</string>   <string name="app_name">雲棲社區</string> </resources>

上面將資源部分分成了3個部分,目的是為了清晰,當然你也可以只建一個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="http://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方法,這裡我的Activity是Study03_01,原始代碼如下:

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方法進行文本顏色的設定,這裡可以有3種方式進行設定:

第1種:

複製代碼 代碼如下:
tv.setTextColor(android.graphics.Color.RED);//系統內建的顏色類

第2種:
複製代碼 代碼如下:
tv.setTextColor(0xffff00ff);//0xffff00ff是int類型的資料
分組一下0x|ff|ff00ff,0x是代表顏色整數的標記,ff是表示透明度,ff00ff表示顏色,注意:這裡ffff00ff必須是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程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.