国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android編程實(shí)現(xiàn)TextView字體顏色設(shè)置的方法小結(jié)

2020-04-11 11:14:27
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了Android編程實(shí)現(xiàn)TextView字體顏色設(shè)置的方法。分享給大家供大家參考,具體如下:

對(duì)于setTextView(int a)這里的a是傳進(jìn)去顏色的值。例如,紅色0xff0000是指0xff0000如何直接傳入R.color.red是沒(méi)有辦法設(shè)置顏色的,只有通過(guò)文章中的第三種方法先拿到資源的顏色值再傳進(jìn)去。

復(fù)制代碼 代碼如下:
tv.setTextColor(this.getResources().getColor(R.color.red));

關(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">地址://m.survivalescaperooms.com/</string>   <string name="app_name">武林網(wǎng)</string> </resources>

上面將資源部分分成了3個(gè)部分,目的是為了清晰,當(dāng)然你也可以只建一個(gè)xml文件放在res目錄下,而且文件名稱可以隨便命名。

注意兩個(gè)地方:

1、main.xml的TextView標(biāo)簽中:

復(fù)制代碼 代碼如下:
android:textColor="@color/red"

2、color.xml中:
復(fù)制代碼 代碼如下:
<color name="red">#FF0000</color>

@color指獲取資源文件中(所有res目錄下的xml文件)的<color>標(biāo)簽

/red指在標(biāo)簽下找其name值為red的內(nèi)容,此時(shí)其值為#FF0000

因此,這里我們還可以這樣做:

復(fù)制代碼 代碼如下:
android:textColor="@drawable/red"

@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種:

復(fù)制代碼 代碼如下:
tv.setTextColor(android.graphics.Color.RED);//系統(tǒng)自帶的顏色類

第2種:
復(fù)制代碼 代碼如下:
tv.setTextColor(0xffff00ff);//0xffff00ff是int類型的數(shù)據(jù)
分組一下0x|ff|ff00ff,0x是代表顏色整數(shù)的標(biāo)記,ff是表示透明度,ff00ff表示顏色,注意:這里ffff00ff必須是8個(gè)的顏色表示,不接受ff00ff這種6個(gè)的顏色表示。

第3種:

復(fù)制代碼 代碼如下:
tv.setTextColor(this.getResources().getColor(R.color.red));//通過(guò)獲得資源文件進(jìn)行設(shè)置。
根據(jù)不同的情況R.color.red也可以是R.string.red或者R.drawable.red,當(dāng)然前提是需要在相應(yīng)的配置文件里做相應(yīng)的配置,如:

<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ì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 华宁县| 海门市| 大埔县| 安福县| 洛浦县| 股票| 工布江达县| 灌南县| 海丰县| 屏山县| 巴彦县| 宜都市| 中方县| 革吉县| 蕲春县| 集贤县| 彭泽县| 图片| 德钦县| 秭归县| 庆阳市| 阳泉市| 兰溪市| 横峰县| 木里| 利辛县| 惠州市| 镇平县| 绥阳县| 夹江县| 确山县| 留坝县| 措勤县| 绥宁县| 濮阳县| 内丘县| 涟水县| 永州市| 夹江县| 富宁县| 出国|