TextView的字體設(shè)置方法:
1、直接通過(guò)配置文件設(shè)置
2、在Activity類中進(jìn)行設(shè)置
先看效果圖:

項(xiàng)目結(jié)構(gòu)圖:

第一種方式很簡(jiǎn)單,用于靜態(tài)或初始文字顏色的設(shè)置,方法如下:
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas./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>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas./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>
- <color name="red">#FF0000</color>
- </resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#FF0000</color>
</resources>
drawable.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>
<?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.</string>
- <string name="app_name">丫
梨的筆記本</string>
- </resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">地址:http://yahaitt.</string>
<string name="app_name">丫梨的筆記本</string>
</resources>
上面將資源部分分成了3個(gè)部分,目的是為了清晰,當(dāng)然你也可以只建一個(gè)xml文件放在res目錄下,而且文件名稱可以隨便命名。
注意兩個(gè)地方:
1、main.xml的TextView標(biāo)簽中:
android:textColor="@color/red"
2、color.xml中:
<color
name="red">#FF0000</color>
@color指獲取資源文件中(所
有res目錄下的xml文件)的<color>標(biāo)簽
/red指在標(biāo)簽下找其name值為red的內(nèi)容,此時(shí)其值為#FF0000
因此,這里我們還可以這樣做:
android:textColor="@drawable/red"
@drawable指獲取資源文件中<drawable>標(biāo)簽
/red指在標(biāo)簽下找其name值為red的內(nèi)容
以此類推,相信你也就知道了如果是在strings.xml中該怎么做了。
|