您的位置:首页 > 其它

Button按钮状态背景的设置

2012-06-21 20:32 302 查看
Android selector选择器可以让你切换自定义的背景风格,让你的控件或者布局在不同状态下背景切换,背景可以使眼色或者图片资源。



首先,android中的selector要在res/drawable/xxx.xml中配置,比如下面Button的例子:

使用drawable:

<?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/item_bg_s" />
<!-- 获取焦点时的背景图 -->
<item android:state_focused="true"
android:drawable="@drawable/item_bg_s" />
<!-- 默认的背景图 -->
<item android:drawable="@drawable/item_bg_n" />
</selector>


这个在java代码中的实现方式是:

Integer[] mButtonState = { R.drawable.defaultbutton,
R.drawable.focusedpressed, R.drawable.pressed };
Button mButton = (Button) findViewById(R.id.button);
mButton.setBackgroundDrawable(myButton.setbg(mButtonState));

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


使用color:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 选中时的颜色 -->
<item android:state_selected="true" android:color="#0FF" />
<!-- 获得焦点时的颜色 -->
<item android:state_focused="true" android:color="#F0F" />
<!-- 点击时的颜色 -->
<item android:state_pressed="true" android:color="#FF0" />
<!-- 默认的颜色 -->
<item android:color="#000" />
</selector>


这个方法可以用来设置Button背景的选择效果,也可以用来设置Button上面文字的选择效果。

最后,将设置好的选择器设置到Button的background中去。

也可以为button增加style,如下

<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:text="按钮"
style="@style/ButtonText"/>


然后在res/values下面定义这个style:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="ButtonText">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#ffffff</item>
<item name="android:gravity">center</item>
<!-- 阴影风格,如粗体,斜体 -->
<item name="android:textStyle">bold</item>
<!-- 阴影颜色,这里可以处理阴影,外发光,描边等效果 -->
<item name="android:shadowColor">#000000</item>
<!-- 阴影x偏移 -->
<item name="android:shadowDx">1</item>
<!-- 阴影y偏移 -->
<item name="android:shadowDy">1</item>
<!-- 阴影半径 -->
<item name="android:shadowRadius">2</item>
<!-- 背景 -->
<item name="android:background">@drawable/customer_button_selector</item>
</style>
</resources>


如果要为按钮加上渐变,圆角,描边等等,可以将selector结合使用shape,这样就能做出更加酷炫的按钮

shape定义按钮的方法和以上类似,可以定义一个selector结和shape:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 按下状态 -->
<item android:state_pressed="true">
<shape>
<solid
android:color="#80ff3434" />
<padding
android:bottom="10dp"
android:left="30dp"
android:right="30dp"
android:top="10dp" />
</shape>
</item>
<!-- 普通状态 -->
<item>
<shape android:shape="rectangle">

<gradient
android:endColor="#ffff1dae"
android:startColor="#ffff79ff"
android:type="linear" />
<padding
android:bottom="10dp"
android:left="30dp"
android:right="30dp"
android:top="10dp" />
</shape>
</item>

</selector>


源码下载:

http://download.csdn.net/detail/zoeice/4420225
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: