Android編程實(shí)現(xiàn)控件不同狀態(tài)文字顯示不同顏色的方法
本文實(shí)例講述了Android編程實(shí)現(xiàn)控件不同狀態(tài)文字顯示不同顏色的方法。分享給大家供大家參考,具體如下:
方式一:
第一要選擇的控件
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/close_time_display" android:layout_marginRight="20dp" android:text="@string/default_time" style="@style/item_content_text_style"/>
style是自定義的風(fēng)格,對(duì)應(yīng)的xml文件如下:
<style name="item_content_text_style">
<item name="android:textSize">26sp</item>
<item name="android:duplicateParentState">true</item>
<item name="android:textColor">@drawable/textcolor_yellow_selector</item>
</style>
textColor中的textcolor_yellow_selector如下
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true" android:color="@color/yellow" /> <item android:state_focused="true" android:color="@color/yellow" /> <item android:state_selected="true" android:color="@color/yellow"></item> <item android:color="@color/white"/> </selector>
實(shí)現(xiàn)方式二:ColorStateList文字變色
API
Windows平臺(tái)VC,對(duì)于不同的按鈕狀態(tài),采用不同的顏色顯示文字,實(shí)現(xiàn)起來(lái)比較復(fù)雜,一般都得自繪按鈕。但是Android里面實(shí)現(xiàn)起來(lái)非常方便。
我們首先添加一個(gè)ColorStateList資源XML文件,XML文件保存在res/color/button_text.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="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
Button btn=(Button)findViewById(R.id.btn);
Resources resource=(Resources)getBaseContext().getResources();
ColorStateList csl=(ColorStateList)resource.getColorStateList(R.color.button_text);
if(csl!=null){
btn.setTextColor(color_state_list);//設(shè)置按鈕文字顏色
}
或者可以這樣:
XmlResourceParser xpp=Resources.getSystem().getXml(R.color.button_text);
try {
ColorStateList csl= ColorStateList.createFromXml(getResources(),xpp);
btn.setTextColor(csl);
} catch (Exception e) {
// TODO: handle exception
}
最后附上所有可能出現(xiàn)的狀態(tài):
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:color="hex_color" android:state_pressed=["true" | "false"] android:state_focused=["true" | "false"] android:state_selected=["true" | "false"] android:state_active=["true" | "false"] android:state_checkable=["true" | "false"] android:state_checked=["true" | "false"] android:state_enabled=["true" | "false"] android:state_window_focused=["true" | "false"] /> </selector>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android編程實(shí)現(xiàn)圖片背景漸變切換與圖層疊加效果
- Android編程實(shí)現(xiàn)左右滑動(dòng)切換背景的方法
- Android實(shí)現(xiàn)動(dòng)態(tài)切換組件背景的方法
- 修改Android FloatingActionButton的title的文字顏色及背景顏色實(shí)例詳解
- Android中EditText和AutoCompleteTextView設(shè)置文字選中顏色方法
- Android開發(fā)中ImageLoder加載網(wǎng)絡(luò)圖片時(shí)將圖片設(shè)置為ImageView背景的方法
- android開發(fā)修改狀態(tài)欄背景色和圖標(biāo)顏色的示例
- Android中創(chuàng)建類似Instagram的漸變背景效果
- Android開發(fā)之背景動(dòng)畫簡(jiǎn)單實(shí)現(xiàn)方法
- Android編程實(shí)現(xiàn)對(duì)話框Dialog背景透明功能示例
- Android開發(fā)實(shí)現(xiàn)按鈕點(diǎn)擊切換背景并修改文字顏色的方法
相關(guān)文章
Flutter利用Hero組件實(shí)現(xiàn)自定義路徑效果的動(dòng)畫
本篇介紹了如何利用Hero動(dòng)畫組件的createRectTween屬性實(shí)現(xiàn)自定義路徑效果的動(dòng)畫。文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-06-06
Android實(shí)現(xiàn)手機(jī)定位的案例代碼
今天小編就為大家分享一篇關(guān)于Android實(shí)現(xiàn)手機(jī)定位的案例代碼,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03
Android字符串資源文件format方法使用實(shí)例
本文介紹了Android的資源文件values/strings.xml中如何實(shí)現(xiàn)格式化字符串,這里舉個(gè)簡(jiǎn)單的例子供大家參考2013-11-11
深入U(xiǎn)nderstanding Android ContentProvider詳解
本篇文章是對(duì)Android ContentProvider進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android Studio如何查看源碼并調(diào)試的方法步驟
這篇文章主要介紹了Android Studio如何查看源碼并調(diào)試的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
Android編程使用pull方式解析xml格式文件的方法詳解
這篇文章主要介紹了Android編程使用pull方式解析xml格式文件的方法,結(jié)合實(shí)例形式分析了Android調(diào)用pull解析器操作xml格式文件的步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-07-07
Android使用系統(tǒng)自帶的相機(jī)實(shí)現(xiàn)一鍵拍照功能
這篇文章主要介紹了Android使用系統(tǒng)自帶的相機(jī)實(shí)現(xiàn)一鍵拍照功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01
Android XMPP通訊自定義Packet&Provider
這篇文章主要介紹了Android XMPP通訊自定義Packet&Provider的相關(guān)資料,需要的朋友可以參考下2016-08-08
Android Studio gradle配置packagingOptions打包so庫(kù)重復(fù)
這篇文章主要為大家介紹了Android Studio gradle配置packagingOptions打包so庫(kù)重復(fù)問(wèn)題的解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

