Android編程設(shè)置TextView顏色setTextColor用法實(shí)例
本文實(shí)例講述了Android編程設(shè)置TextView顏色setTextColor用法。分享給大家供大家參考,具體如下:
android中設(shè)置TextView的顏色有方法setTextColor,這個(gè)方法被重載了,可以傳入兩種參數(shù)。
public void setTextColor(int color) {
mTextColor = ColorStateList.valueOf(color);
updateTextColors();
}
public void setTextColor(ColorStateList colors) {
if (colors == null) {
throw new NullPointerException();
}
mTextColor = colors;
updateTextColors();
}
下邊就分別寫(xiě)出怎么使用這兩個(gè)方法設(shè)置TextView的顏色:
TextView tv = new TextView(this);
tv.setText("Test set TextView's color.");
//方案一:代碼中通過(guò)argb值的方式
tv.setTextColor(Color.rgb(255, 255, 255));
這種方法也就是傳入int color值,這個(gè)int不是R文件中自動(dòng)分配的int值,所以要注意。這是Color類(lèi)中的靜態(tài)方法構(gòu)造出來(lái)的顏色int值。
Resources resource = (Resources) getBaseContext().getResources();
ColorStateList csl = (ColorStateList) resource.getColorStateList(R.color.my_color);
if (csl != null) {
tv.setTextColor(csl);
}
這種方法是通過(guò)ColorStateList得到xml中的配置的顏色的。好多需要xml中配置的都要類(lèi)似這樣的映射xml文件。
還有種方法:
XmlResourceParser xrp = getResources().getXml(R.color.my_color);
try {
ColorStateList csl = ColorStateList.createFromXml(getResources(), xrp);
tv.setTextColor(csl);
} catch (Exception e) {
}
全部代碼:
package com.txlong;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
public class ListViewDemoActivity extends Activity {
// private ListView listView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Test set TextView's color.");
//方案一:通過(guò)ARGB值的方式
/**
* set the TextView color as the 0~255's ARGB,These component values
* should be [0..255], but there is no range check performed, so if they
* are out of range, the returned color is undefined
*/
// tv.setTextColor(Color.rgb(255, 255, 255));
/**
* set the TextView color as the #RRGGBB #AARRGGBB 'red', 'blue',
* 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow',
* 'lightgray', 'darkgray'
*/
tv.setTextColor(Color.parseColor("#FFFFFF"));
/** 原來(lái)不知道有上邊的方法,就用這個(gè)笨笨方法了 */
// String StrColor = null;
// StrColor = "FFFFFFFF";
// int length = StrColor.length();
// if (length == 6) {
// tv.setTextColor(Color.rgb(
// Integer.valueOf(StrColor.substring(0, 2), 16),
// Integer.valueOf(StrColor.substring(2, 4), 16),
// Integer.valueOf(StrColor.substring(4, 6), 16)));
// } else if (length == 8) {
// tv.setTextColor(Color.argb(
// Integer.valueOf(StrColor.substring(0, 2), 16),
// Integer.valueOf(StrColor.substring(2, 4), 16),
// Integer.valueOf(StrColor.substring(4, 6), 16),
// Integer.valueOf(StrColor.substring(6, 8), 16)));
// }
//方案二:通過(guò)資源引用
// tv.setTextColor(getResources().getColor(R.color.my_color));
//方案三:通過(guò)資源文件寫(xiě)在String.xml中
// Resources resource = (Resources) getBaseContext().getResources();
// ColorStateList csl = (ColorStateList) resource.getColorStateList(R.color.my_color);
// if (csl != null) {
// tv.setTextColor(csl);
// }
//方案四:通過(guò)xml文件,如/res/text_color.xml
// XmlPullParser xrp = getResources().getXml(R.color.text_color);
// try {
// ColorStateList csl = ColorStateList.createFromXml(getResources(), xrp);
// tv.setTextColor(csl);
// } catch (Exception e) {
// }
// listView = new ListView(this);
//
// Cursor cursor = getContentResolver().query(
// Uri.parse("content://contacts/people"), null, null, null, null);
//
// startManagingCursor(cursor);
//
// ListAdapter listAdapter = new SimpleCursorAdapter(this,
// android.R.layout.simple_expandable_list_item_2, cursor,
// new String[] { "name", "name" }, new int[] {
// android.R.id.text1, android.R.id.text2 });
//
// listView.setAdapter(listAdapter);
// setContentView(listView);
setContentView(tv);
}
}
String.xml文件為:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, ListViewDemoActivity!</string> <string name="app_name">ListViewDemo</string> <color name="my_color">#FFFFFF</color> </resources>
/res/color/text_color.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:color="#FF111111"/> <!-- pressed --> <item android:state_focused="true" android:color="#FF222222"/> <!-- focused --> <item android:state_selected="true" android:color="#FF333333"/> <!-- selected --> <item android:state_active="true" android:color="#FF444444"/> <!-- active --> <item android:state_checkable="true" android:color="#FF555555"/> <!-- checkable --> <item android:state_checked="true" android:color="#FF666666"/> <!-- checked --> <item android:state_enabled="true" android:color="#FF777777"/> <!-- enabled --> <item android:state_window_focused="true" android:color="#FF888888"/> <!-- window_focused --> </selector>
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android取消EditText自動(dòng)獲取焦點(diǎn)默認(rèn)行為
- Android控件系列之EditText使用方法
- android同時(shí)控制EditText輸入字符個(gè)數(shù)和禁止特殊字符輸入的方法
- Android中EditText實(shí)現(xiàn)不可編輯解決辦法
- Android定制自己的EditText輕松改變底線顏色
- Android更改EditText下劃線顏色樣式的方法
- Android 設(shè)置Edittext獲取焦點(diǎn)并彈出軟鍵盤(pán)
- 全面解析Android中對(duì)EditText輸入實(shí)現(xiàn)監(jiān)聽(tīng)的方法
- android基礎(chǔ)教程之a(chǎn)ndroid的listview與edittext沖突解決方法
- Android中EditText setText方法的踩坑實(shí)戰(zhàn)
相關(guān)文章
RecyclerView實(shí)現(xiàn)側(cè)滑和網(wǎng)絡(luò)斷點(diǎn)續(xù)傳
這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)側(cè)滑和網(wǎng)絡(luò)斷點(diǎn)續(xù)傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
Android中使用Canvas繪制南丁格爾玫瑰圖(Nightingale rose diagram)
這篇文章主要介紹了Android中使用Canvas繪制南丁格爾玫瑰圖(Nightingale rose diagram),本文直接給出實(shí)現(xiàn)代碼和運(yùn)行效果圖,需要的朋友可以參考下2015-03-03
Ubuntu中為Android HAL編寫(xiě)JNI方法提供JAVA訪問(wèn)硬件服務(wù)接口
本文主要介紹Ubuntu中為Android硬件抽象層模塊編寫(xiě)JNI方法提供Java訪問(wèn)硬件服務(wù)接口,這里給大家詳細(xì)說(shuō)明如何編寫(xiě) JNI方法訪問(wèn)硬件接口并附示例代碼,有需要的小伙伴參考下2016-08-08
Android中通過(guò)AsyncTask類(lèi)來(lái)制作炫酷進(jìn)度條的實(shí)例教程
這篇文章主要介紹了Android中通過(guò)AsyncTask來(lái)制作炫酷進(jìn)度條的實(shí)例教程,借助AsyncTask類(lèi)的線程操作方法來(lái)管理異步任務(wù),需要的朋友可以參考下2016-05-05
解決EditText不顯示光標(biāo)的三種方法(總結(jié))
下面小編就為大家?guī)?lái)一篇解決EditText不顯示光標(biāo)的三種方法(總結(jié))。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
Android開(kāi)發(fā)之開(kāi)發(fā)者頭條APP(三)實(shí)現(xiàn)首頁(yè)
這篇文章主要介紹了Android開(kāi)發(fā)之開(kāi)發(fā)者頭條APP(三)實(shí)現(xiàn)首頁(yè)的相關(guān)資料,需要的朋友可以參考下2016-04-04
Android?Studio實(shí)現(xiàn)簡(jiǎn)單繪圖板
這篇文章主要為大家詳細(xì)介紹了Android?Studio實(shí)現(xiàn)簡(jiǎn)單繪圖板,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05

