android背景选择器selector使用方法

fmms 12年前

方法一:代码实现</strong></p>

1. 自定义状态效果可以通过代码实现,也可以通过xml定义style实现。

2. 下面先介绍代码实现,通过StateListDrawable定义Button背景。

3. 由于View类中PRESSED_ENABLED_STATE_SET值不是公共常量,所以通过继承来访问了。

特注:其他控件的效果,比如ImageView,也可以通过这种方法实现,但是由于ImageView默认是没焦点,不可点击的,需要自己更改(需要点击就设置android:clickable="true" , 需要能够选中就设置android:focusable="true" )。

java 代码:

package com.test.TestButton;     import android.app.Activity;  import android.content.Context;  import android.graphics.drawable.Drawable;  import android.graphics.drawable.StateListDrawable;  import android.os.Bundle;  import android.view.View;  import android.widget.Button;     public class TestButton extends Activity {      @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);          Integer[] mButtonState = { R.drawable.defaultbutton,                  R.drawable.focusedpressed, R.drawable.pressed };          Button mButton = (Button) findViewById(R.id.button);          MyButton myButton = new MyButton(this);          mButton.setBackgroundDrawable(myButton.setbg(mButtonState));      }         class MyButton extends View {             public MyButton(Context context) {              super(context);          }             // 以下这个方法也可以把你的图片数组传过来,以StateListDrawable来设置图片状态,来表现button的各中状态。未选          // 中,按下,选中效果。          public StateListDrawable setbg(Integer[] mImageIds) {              StateListDrawable bg = new StateListDrawable();              Drawable normal = this.getResources().getDrawable(mImageIds[0]);              Drawable selected = this.getResources().getDrawable(mImageIds[1]);              Drawable pressed = this.getResources().getDrawable(mImageIds[2]);              bg.addState(View.PRESSED_ENABLED_STATE_SET, pressed);              bg.addState(View.ENABLED_FOCUSED_STATE_SET, selected);              bg.addState(View.ENABLED_STATE_SET, normal);              bg.addState(View.FOCUSED_STATE_SET, selected);              bg.addState(View.EMPTY_STATE_SET, normal);              return bg;          }      }  }

</span>

XML代码:

在res/drawable下面新建mybutton_background.xml文件,内容如下:

<?xml version=”1.0″ encoding=”utf-8″?>  <selector xmlns:android=”http://schemas.android.com/apk/res/android“>     <item android:state_focused=”true”                 android:state_pressed=”false”               android:drawable=”@drawable/yellow” />    <item android:state_focused=”true”                  android:state_pressed=”true”                 android:drawable=”@drawable/green” />  <item android:state_focused=”false”                  android:state_pressed=”true”            android:drawable=”@drawable/blue” />   <item android:drawable=”@drawable/grey” />  </selector>

这里面就定义了在不同状态下的显示图片,然后在layout里面定义Button的时候,指定它的background为这个mybutton_background

<?xml version=”1.0″ encoding=”utf-8″?>  <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”          android:orientation=”vertical”          android:layout_width=”fill_parent”          android:layout_height=”fill_parent”          >      <Button android:id=”@+id/btn”              android:layout_width=”wrap_content”              android:layout_height=”wrap_content”              android:text=”@string/mybtn”              android:background=”@drawable/mybutton_background” />  </LinearLayout>

这种方式开发比较简单,适合做一些风格一致的Button,设置成同一个background就可以了。ImageView等控件如方法一中所述。