Android編程實現(xiàn)TextView字體顏色設(shè)置的方法小結(jié)
本文實例講述了Android編程實現(xiàn)TextView字體顏色設(shè)置的方法。分享給大家供大家參考,具體如下:
對于setTextView(int a)這里的a是傳進(jìn)去顏色的值。例如,紅色0xff0000是指0xff0000如何直接傳入R.color.red是沒有辦法設(shè)置顏色的,只有通過文章中的第三種方法先拿到資源的顏色值再傳進(jìn)去。
關(guān)鍵字: android textview color
TextView的字體設(shè)置方法:
1、直接通過配置文件設(shè)置
2、在Activity類中進(jìn)行設(shè)置
第一種方式很簡單,用于靜態(tài)或初始文字顏色的設(shè)置,方法如下:
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.dhdzp.com/</string> <string name="app_name">腳本之家</string> </resources>
上面將資源部分分成了3個部分,目的是為了清晰,當(dāng)然你也可以只建一個xml文件放在res目錄下,而且文件名稱可以隨便命名。
注意兩個地方:
1、main.xml的TextView標(biāo)簽中:
2、color.xml中:
@color指獲取資源文件中(所有res目錄下的xml文件)的<color>標(biāo)簽
/red指在標(biāo)簽下找其name值為red的內(nèi)容,此時其值為#FF0000
因此,這里我們還可以這樣做:
@drawable指獲取資源文件中<drawable>標(biāo)簽
/red指在標(biāo)簽下找其name值為red的內(nèi)容
以此類推,相信你也就知道了如果是在strings.xml中該怎么做了。
下面看看第二種方式:在Activity類中進(jìn)行設(shè)置
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方法進(jìn)行文本顏色的設(shè)置,這里可以有3種方式進(jìn)行設(shè)置:
第1種:
第2種:
第3種:
<color name="red">#FF0000</color> <drawable name="red">#FF0000</drawable> <string name="red">#FF0000</string>
詳細(xì)的代碼如下:
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程序設(shè)計有所幫助。
相關(guān)文章
Android自定義Animation實現(xiàn)View搖擺效果
這篇文章主要為大家詳細(xì)介紹了Android自定義Animation實現(xiàn)View搖擺效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01
深入分析Android NFC技術(shù) android nfc開發(fā)
本篇文章我們對android開發(fā)中nfc技術(shù)做了全面的原理分析以及實現(xiàn)過程,需要的讀者們一起參考一下吧。2017-11-11
Android改變ExpandableListView的indicator圖標(biāo)實現(xiàn)方法
這篇文章主要介紹了Android改變ExpandableListView的indicator圖標(biāo)實現(xiàn)方法,結(jié)合實例形式分析了改變ExpandableListView的indicator圖標(biāo)相關(guān)步驟與實現(xiàn)技巧,涉及Android配置文件的修改,需要的朋友可以參考下2016-03-03
Android ActionBar完全解析使用官方推薦的最佳導(dǎo)航欄(上)
Action Bar是一種新増的導(dǎo)航欄功能,在Android 3.0之后加入到系統(tǒng)的API當(dāng)中,它標(biāo)識了用戶當(dāng)前操作界面的位置,并提供了額外的用戶動作、界面導(dǎo)航等功能2017-04-04
Windows下快速搭建安卓開發(fā)環(huán)境Android studio
這篇文章主要介紹了Windows下快速搭建安卓開發(fā)環(huán)境Android studio的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-07-07
Android編程中沉浸式狀態(tài)欄的三種實現(xiàn)方式詳解
這篇文章主要介紹了Android編程中沉浸式狀態(tài)欄的三種實現(xiàn)方式,簡單描述了沉浸式狀態(tài)欄的概念、功能并結(jié)合實例形式詳細(xì)分析了Android實現(xiàn)沉浸式狀態(tài)欄的三種操作技巧與注意事項,需要的朋友可以參考下2018-02-02

