1. 程式人生 > >改變android控制元件顏色

改變android控制元件顏色

許多應用都需要根據使用者的操作來改變圖形顏色,文字顏色。 最多的應用場景包括,當用戶將焦點移動到某一個模組時,高亮那個模組。 當用戶點選某一個按鈕時,改變按鈕上文字的顏色和按鈕的背景色。 1:根據使用者操作改變按鈕的背景色(圖) 先以改變按鈕的背景色為例子。 首先,你應該有按鈕的這幾種對應效果圖。我這有三種 聚焦:btn_focus.9.png  按壓:btn_press.9.png  預設:btn.9.png 這三張圖都放到drawable中。然後在drawable中建立一個任意名的xml檔案比如 res/drawable/blue_orange_btn.xml <?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">     <item android:state_pressed="true"    ---------按壓時         android:drawable="@drawable/btn_press" />     <item android:state_focused="true"    ---------聚焦時         android:drawable="@drawable/btn_focus" />     <item                                 ---------預設時         android:drawable="@drawable/btn" /> </selector> 這個排序是有講究的,因為android在匹配效果的時候是從上往下的,一旦有匹配的就不會再往下匹配了。所以在控制類似checkbox的圖形屬性時,除了可以用多個android:state來進行精確的控制外,還要注意排序。 android:state_checked="true" android:state_focused="true"詳細的checkbox設定我之前的blog寫過 就不贅述了。 按照經驗和慣例,一般是呈現給使用者時間越長的越排在這個xml的底部。
在這個button的layout檔案中如下設定 <Button android:id="@+id/begin_btn" android:layout_width="258dp" style="@style/text_18sp_fff.22sp" android:text="@string/begin_btn" android:layout_centerHorizontal="true" android:layout_below="@id/diban" android:layout_marginTop="16dp" android:background="@drawable/blue_orange_btn"
/> 1.2:動態的改變使用者操作背景色組合 根據不同的需求 你還可以在程式碼中靈活的控制不同的色彩組合。 比如你有兩套顏色變化: res/drawable/blue_orange_btn.xml res/drawable/blue_white_btn.xml 你可以在程式碼中按照你的需求動態設定。 if(begin){ begin_btn.setBackgroundResource(R.drawable.blue_orange_btn); }else{ begin_btn.setBackgroundResource(R.drawable.blue_white_btn); } 如果用的不是button 而是用imageView 你可以用setImageResource(int)方法,具體的看文件吧。 2: 根據使用者操作改變文字的顏色。
改變文字顏色的操作方法跟改變按鈕或ImageView的理論是一樣的。 建立res/color/text_white_blue.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="@android:color/white" />     <item android:state_focused="true"         android:color="@android:color/white" />     <item          android:color="@android:color/blue" /> </selector> layout中TextView如下 <TextView     android:id="@+id/call_log"     style="@style/style1" android:duplicateParentState="true"     android:text="@string/call_log"        android:textColor="@color/text_white_blue"    /> 中間有一個duplicateParentState屬性。主要作用是當這個TextView是屬於某一個控制元件的時候,比如屬於一個layout。他會保持跟父控制元件的使用者操作狀態一致。 也就是說當這個TextView所屬於的layout被使用者所按壓的時候,這個TextView會同時被至於按壓的狀態。 這個屬效能夠極大的簡化程式設計師的工作量,可以讓一個主控制元件和其子控制元件保持步調一致改變,統一風格。 2.2:在程式碼中動態的設定使用者操作的文字色組合。 本來以為會跟button 和 ImageView一樣非常簡單就是對應的set方法加上資原始檔。 但是其實不是的,字型的顏色如果用setTextColor(R.color.text_blue_white)來設定,會悲劇。 原因我也不是很明白,反正幾乎是改變不了。 程式碼中正確的設定方式應該是 if(begin) setTextColor(getResources().getColor(R.color.text_white_blue)); else      setTextColor(getResources().getColor(R.color.text_white_red)); By the way: 有時候需要將整個textview中的部分文字改變顏色 簡單的辦法如下 TextView.append(Html.fromHtml("<font color=\"#0088bb\">"+str+"</font>")); 當然也有更復雜和更強大的方法,就是通過SpannableString 簡單舉例如下     SpannableString ss = new SpannableString("AAAA.");
         //改變0-2號字元的顏色         ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 2,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);