Android編程實(shí)現(xiàn)TextView字體顏色設(shè)置的方法小結(jié)
本文實(shí)例講述了Android編程實(shí)現(xiàn)TextView字體顏色設(shè)置的方法。分享給大家供大家參考,具體如下:
對(duì)于setTextView(int a)這里的a是傳進(jìn)去顏色的值。例如,紅色0xff0000是指0xff0000如何直接傳入R.color.red是沒有辦法設(shè)置顏色的,只有通過(guò)文章中的第三種方法先拿到資源的顏色值再傳進(jìn)去。
關(guān)鍵字: android textview color
TextView的字體設(shè)置方法:
1、直接通過(guò)配置文件設(shè)置
2、在Activity類中進(jìn)行設(shè)置
第一種方式很簡(jiǎn)單,用于靜態(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.fzitv.net/</string> <string name="app_name">腳本之家</string> </resources>
上面將資源部分分成了3個(gè)部分,目的是為了清晰,當(dāng)然你也可以只建一個(gè)xml文件放在res目錄下,而且文件名稱可以隨便命名。
注意兩個(gè)地方:
1、main.xml的TextView標(biāo)簽中:
2、color.xml中:
@color指獲取資源文件中(所有res目錄下的xml文件)的<color>標(biāo)簽
/red指在標(biāo)簽下找其name值為red的內(nèi)容,此時(shí)其值為#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
第二步:通過(guò)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);
}
}
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android自定義Animation實(shí)現(xiàn)View搖擺效果
這篇文章主要為大家詳細(xì)介紹了Android自定義Animation實(shí)現(xiàn)View搖擺效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
深入分析Android NFC技術(shù) android nfc開發(fā)
本篇文章我們對(duì)android開發(fā)中nfc技術(shù)做了全面的原理分析以及實(shí)現(xiàn)過(guò)程,需要的讀者們一起參考一下吧。2017-11-11
Android星級(jí)評(píng)分條控件RatingBar使用詳解
這篇文章主要為大家詳細(xì)介紹了Android星級(jí)評(píng)分條控件RatingBar的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Android自定義View實(shí)現(xiàn)角度選擇器
前幾天在Google Photos查看照片,用了一下它的圖片剪裁功能,于是我馬上就被其界面和操作吸引。后來(lái)想模仿做一個(gè)和Google Photos裁圖頁(yè)面幾乎一模一樣的角度選擇器,本文比較基礎(chǔ),在閱讀本文前只需要掌握最基礎(chǔ)的自定義View知識(shí)和Android事件知識(shí)。下面來(lái)一起學(xué)習(xí)下吧。2016-11-11
Android改變ExpandableListView的indicator圖標(biāo)實(shí)現(xiàn)方法
這篇文章主要介紹了Android改變ExpandableListView的indicator圖標(biāo)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了改變ExpandableListView的indicator圖標(biāo)相關(guān)步驟與實(shí)現(xiàn)技巧,涉及Android配置文件的修改,需要的朋友可以參考下2016-03-03
Android ActionBar完全解析使用官方推薦的最佳導(dǎo)航欄(上)
Action Bar是一種新増的導(dǎo)航欄功能,在Android 3.0之后加入到系統(tǒng)的API當(dāng)中,它標(biāo)識(shí)了用戶當(dāng)前操作界面的位置,并提供了額外的用戶動(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)欄的三種實(shí)現(xiàn)方式詳解
這篇文章主要介紹了Android編程中沉浸式狀態(tài)欄的三種實(shí)現(xiàn)方式,簡(jiǎn)單描述了沉浸式狀態(tài)欄的概念、功能并結(jié)合實(shí)例形式詳細(xì)分析了Android實(shí)現(xiàn)沉浸式狀態(tài)欄的三種操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-02-02

