欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程實現(xiàn)控件不同狀態(tài)文字顯示不同顏色的方法

 更新時間:2016年02月24日 09:25:09   作者:炫_愛羊  
這篇文章主要介紹了Android編程實現(xiàn)控件不同狀態(tài)文字顯示不同顏色的方法,涉及Android針對控件布局文件屬性設(shè)置及狀態(tài)判定等相關(guān)技巧,需要的朋友可以參考下

本文實例講述了Android編程實現(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)格,對應(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>

實現(xiàn)方式二:ColorStateList文字變色

API

Windows平臺VC,對于不同的按鈕狀態(tài),采用不同的顏色顯示文字,實現(xiàn)起來比較復(fù)雜,一般都得自繪按鈕。但是Android里面實現(xiàn)起來非常方便。

我們首先添加一個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é)

希望本文所述對大家Android程序設(shè)計有所幫助。

相關(guān)文章

  • Android自定義仿微信PopupWindow效果

    Android自定義仿微信PopupWindow效果

    這篇文章主要為大家詳細(xì)介紹了Android自定義仿微信PopupWindow效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • 最新評論