android實用技巧
阿新 • • 發佈:2019-02-16
眾所周知,android可以通過XML檔案來建立selector,以Drawable物件的形式安裝到元件上,以提供統一的風格設定。但是在某些時候,我們需要通過程式碼的形式來實現相同的功能,例如元件數量非常多,對應不同的圖片,這時候如果還用XML的話就需要建立大量的selector檔案,非常繁瑣。
例如一個TextView使用瞭如下的selector
<TextView android:id="@+id/TextView_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusable="true" android:drawableTop="@drawable/selector_tabwidget_icon" android:textAlignment="center" />
<selector xmlns:android="http://schemas.android.com/apk/res/android" > <!-- Non focused states --> <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/contact" /> <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/contact_sel" /> <!-- Focused states --> <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/contact_sel" /> <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/contact_sel" /> <!-- Pressed --> <item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/contact_sel" /> <item android:state_pressed="true" android:drawable="@drawable/contact_sel" /> </selector>
裡面所引用的圖片資原始檔非常多,如果每個檔案都對應一個XML的檔案的話,就會非常繁瑣,修改起來非常麻煩。
實際上,所有XML設定能做的事情,android裡同樣可以用編碼的方式來實現,像上面那個XML檔案,就可以就下面的程式碼來實現:
StateListDrawable drawable = new StateListDrawable(); //Non focused states drawable.addState(new int[]{-android.R.attr.state_focused, -android.R.attr.state_selected, -android.R.attr.state_pressed}, getResources().getDrawable(R.drawable.contact)); drawable.addState(new int[]{-android.R.attr.state_focused, android.R.attr.state_selected, -android.R.attr.state_pressed}, getResources().getDrawable(R.drawable.contact_sel)); //Focused states drawable.addState(new int[]{android.R.attr.state_focused,-android.R.attr.state_selected, -android.R.attr.state_pressed}, getResources().getDrawable(R.drawable.contact_sel)); drawable.addState(new int[]{android.R.attr.state_focused,android.R.attr.state_selected, -android.R.attr.state_pressed}, getResources().getDrawable(R.drawable.contact_sel)); //Pressed drawable.addState(new int[]{android.R.attr.state_selected, android.R.attr.state_pressed}, getResources().getDrawable(R.drawable.contact_sel)); drawable.addState(new int[]{android.R.attr.state_pressed}, getResources().getDrawable(R.drawable.contact_sel)); TextView textView = (TextView) findViewById(R.id.TextView_title); textView.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
注意裡面的“-”號,當XML的設定是false時,就需要使用資源符號的負值來設定
- package lab.sodino.statelist;
- import android.app.Activity;
- import android.content.Context;
- import android.content.res.ColorStateList;
- import android.graphics.drawable.Drawable;
- import android.graphics.drawable.StateListDrawable;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.TextView;
- /**
- * 對TextView設定ColorStateList使其在Normal、Pressed、Focused、Unable四種狀態下顯示不同的顏色。<br/>
- * StateListDrawable可直接使用圖片應用在相似場合。
- */
- publicclass ActColorStateList extends Activity implements OnClickListener {
- private TextView txtShow;
- publicvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- txtShow = (TextView) findViewById(R.id.txtShow);
- txtShow.setText("Sodino\nNormal:0xffffffff\nPressed:0xffffff00\nFocused:0xff0000ff\nUnable:0xffff0000");
- txtShow.setTextColor(createColorStateList(0xffffffff, 0xffffff00, 0xff0000ff, 0xffff0000));
- txtShow.setOnClickListener(this);
- }
- /** 對TextView設定不同狀態時其文字顏色。 */
- private ColorStateList createColorStateList(int normal, int pressed, int focused, int unable) {
- int[] colors = newint[] { pressed, focused, normal, focused, unable, normal };
- int[][] states = newint[6][];
- states[0] = newint[] { android.R.attr.state_pressed, android.R.attr.state_enabled };
- states[1] = newint[] { android.R.attr.state_enabled, android.R.attr.state_focused };
- states[2] = newint[] { android.R.attr.state_enabled };
- states[3] = newint[] { android.R.attr.state_focused };
- states[4] = newint[] { android.R.attr.state_window_focused };
- states[5] = newint[] {};
- ColorStateList colorList = new ColorStateList(states, colors);
- return colorList;
- }
- /** 設定Selector。 */
- publicstatic StateListDrawable newSelector(Context context, int idNormal, int idPressed, int idFocused,
- int idUnable) {
- StateListDrawable bg = new StateListDrawable();
- Drawable normal = idNormal == -1 ? null : context.getResources().getDrawable(idNormal);
- Drawable pressed = idPressed == -1 ? null : context.getResources().getDrawable(idPressed);
- Drawable focused = idFocused == -1 ? null : context.getResources().getDrawable(idFocused);
- Drawable unable = idUnable == -1 ? null : context.getResources().getDrawable(idUnable);
- // View.PRESSED_ENABLED_STATE_SET
- bg.addState(newint[] { android.R.attr.state_pressed, android.R.attr.state_enabled }, pressed);
- // View.ENABLED_FOCUSED_STATE_SET
- bg.addState(newint[] { android.R.attr.state_enabled, android.R.attr.state_focused }, focused);
- // View.ENABLED_STATE_SET
- bg.addState(newint[] { android.R.attr.state_enabled }, normal);
- // View.FOCUSED_STATE_SET
- bg.addState(newint[] { android.R.attr.state_focused }, focused);
- // View.WINDOW_FOCUSED_STATE_SET
- bg.addState(newint[] { android.R.attr.state_window_focused }, unable);
- // View.EMPTY_STATE_SET
- bg.addState(newint[] {}, normal);
- return bg;
- }
- @Override
- publicvoid onClick(View v) {
- if (v == txtShow) {
- txtShow.setEnabled(false);
- }
- }
- }