android背景选择器selector使用方法

方法一:代码实现java

1. 自定义状态效果能够经过代码实现,也能够经过xml定义style实现。android

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

3. 因为View类中PRESSED_ENABLED_STATE_SET值不是公共常量,因此经过继承来访问了。app

特注:其余控件的效果,好比ImageView,也能够经过这种方法实现,可是因为ImageView默认是没焦点,不可点击的,须要本身更改(须要点击就设置android:clickable="true" , 须要可以选中就设置android:focusable="true" )。ide

java 代码:this

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;
        }
    }
}

XML代码:spa

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

<?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_backgroundorm

<?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等控件如方法一中所述。xml

相关文章
相关标签/搜索