Android TextView字體顏色設置方法小結
本文實例總結了Android TextView字體顏色設置方法。分享給大家供大家參考,具體如下:
對于setTextView(int a)這里的a是傳進去顏色的值。例如,紅色0xff0000是指0xff0000如何直接傳入R.color.red是沒有辦法設置顏色的,只有通過文章中的第三種方法先拿到資源的顏色值再傳進去。
tv.setTextColor(this.getResources().getColor(R.color.red));
關鍵字: android textview color
TextView的字體設置方法:
1、直接通過配置文件設置
2、在Activity類中進行設置
第一種方式很簡單,用于靜態(tài)或初始文字顏色的設置,方法如下:
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://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的內(nèi)容,此時其值為#FF0000
因此,這里我們還可以這樣做:android:textColor="@drawable/red"
@drawable指獲取資源文件中<drawable>標簽
/red指在標簽下找其name值為red的內(nèi)容
以此類推,相信你也就知道了如果是在strings.xml中該怎么做了。
下面看看第二種方式:在Activity類中進行設置
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方法進行文本顏色的設置,這里可以有3種方式進行設置:
第1種:tv.setTextColor(android.graphics.Color.RED);//系統(tǒng)自帶的顏色類
第2種:tv.setTextColor(0xffff00ff);//0xffff00ff是int類型的數(shù)據(jù),分組一下0x|ff|ff00ff,0x是代表顏色整數(shù)的標記,ff是表示透明度,ff00ff表示顏色,注意:這里ffff00ff必須是8個的顏色表示,不接受ff00ff這種6個的顏色表示。
第3種:tv.setTextColor(this.getResources().getColor(R.color.red));//通過獲得資源文件進行設置。根據(jù)不同的情況R.color.red也可以是R.string.red或者R.drawable.red,當然前提是需要在相應的配置文件里做相應的配置,如:
<color name="red">#FF0000</color> <drawable name="red">#FF0000</drawable> <string name="red">#FF0000</string>
詳細的代碼如下:
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相關內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android通信方式總結》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
- android TextView設置中文字體加粗實現(xiàn)方法
- Android TextView設置背景色與邊框的方法詳解
- Android編程開發(fā)之TextView文字顯示和修改方法(附TextView屬性介紹)
- android實現(xiàn)上下滾動的TextView
- android TextView多行文本(超過3行)使用ellipsize屬性無效問題的解決方法
- android TextView不用ScrollViewe也可以滾動的方法
- Android設置TextView顯示指定個數(shù)字符,超過部分顯示...(省略號)的方法
- Android中TextView顯示圓圈背景或設置圓角的方法
- Android中TextView實現(xiàn)分段顯示不同顏色的字符串
- Android?妙用TextView實現(xiàn)左邊文字,右邊圖片
相關文章
Android studio 引用aar 進行java開發(fā)的操作步驟
這篇文章主要介紹了Android studio 引用aar 進行java開發(fā)的操作步驟,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09Android 中 GridView嵌套在ScrollView里只有一行的解決方法
本文給大家?guī)韮煞N有關Android 中 GridView嵌套在ScrollView里只有一行的解決方法,非常不錯,具有參考借鑒價值,感興趣的朋友一起看看吧2016-10-10Android webview 內(nèi)存泄露的解決方法
這篇文章主要介紹了Android webview 內(nèi)存泄露的解決方法的相關資料,需要的朋友可以參考下2017-07-07