读书人

java代码设立selector

发布时间: 2012-08-22 09:50:35 作者: rapoo

java代码设置selector
imageButton.setImageDrawable(m_Selector.setBackground(R.drawable.button_cancelfavorites, R.drawable.button_cancelfavorites, R.drawable.button_cancelfavorites_pressed));


public class Selector extends View{

public Selector(Context context) {

super(context);

}

/**

* 以下这个方法也可以把你的图片数组传过来,以StateListDrawable来设置图片状态,

* 来表现button的各中状态。未选 中,按下,选中效果。

*

* @param normalResId 正常的图片资源id

* @param pressedResId 按下的图片资源id

* @param focusedResId 获得焦点的图片资源id

* @return

*/

public StateListDrawable setBackground(int normalResId, int pressedResId, int focusedResId) {

StateListDrawable bg = new StateListDrawable();

Drawable normal = getResources().getDrawable(normalResId);

Drawable selected = getResources().getDrawable(focusedResId);

Drawable pressed = getResources().getDrawable(pressedResId);

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;


}
}

读书人网 >移动开发

热点推荐