本文實例總結了android textview字體顏色設置方法。分享給大家供大家參考,具體如下:
對于settextview(int a)這里的a是傳進去顏色的值。例如,紅色0xff0000是指0xff0000如何直接傳入r.color.red是沒有辦法設置顏色的,只有通過文章中的第三種方法先拿到資源的顏色值再傳進去。
1
|
tv.settextcolor( this .getresources().getcolor(r.color.red)); |
關鍵字: android textview color
textview的字體設置方法:
1、直接通過配置文件設置
2、在activity類中進行設置
第一種方式很簡單,用于靜態或初始文字顏色的設置,方法如下:
main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?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
1
2
3
4
5
6
7
8
9
10
11
12
|
<?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: //yahaitt.javaeye.com</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":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?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,原始代碼如下:
1
2
3
4
5
6
7
8
9
|
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,當然前提是需要在相應的配置文件里做相應的配置,如:
1
2
3
|
<color name= "red" >#ff0000</color> <drawable name= "red" >#ff0000</drawable> <string name= "red" >#ff0000</string> |
詳細的代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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程序設計有所幫助。