Button按钮状态背景的设置
2012-06-21 20:32
302 查看
Android selector选择器可以让你切换自定义的背景风格,让你的控件或者布局在不同状态下背景切换,背景可以使眼色或者图片资源。
![](http://my.csdn.net/uploads/201207/10/1341888430_9960.png)
首先,android中的selector要在res/drawable/xxx.xml中配置,比如下面Button的例子:
使用drawable:
这个在java代码中的实现方式是:
使用color:
这个方法可以用来设置Button背景的选择效果,也可以用来设置Button上面文字的选择效果。
最后,将设置好的选择器设置到Button的background中去。
也可以为button增加style,如下
然后在res/values下面定义这个style:
如果要为按钮加上渐变,圆角,描边等等,可以将selector结合使用shape,这样就能做出更加酷炫的按钮
shape定义按钮的方法和以上类似,可以定义一个selector结和shape:
源码下载:
http://download.csdn.net/detail/zoeice/4420225
![](http://my.csdn.net/uploads/201207/10/1341888430_9960.png)
首先,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
相关文章推荐
- Button按钮状态背景的设置
- 设置Button按钮状态背景
- 设置button不同状态下的背景色,即把这个颜色变成图片设置成,背景图片
- Android之Button文本及背景根据状态设置
- 去掉button按钮左右两边的留白 设置按钮背景为透明 去掉鼠标点击时出现的虚线边框 层的显示与隐藏
- 【经验小谈】设置按钮点击背景 button设置点击背景 控件设置切换背景 找不到@color 没有@color属性 没有color属性
- 设置button的背景图,当按钮没有点中和没被点中分别显示不同的效果
- Android之自定义背景Button按钮、自定义形状Button的全攻略、设置字体颜色
- Android利用selector设置按钮不同状态下的背景图片
- Android selector的用法及设置按钮不同状态下的背景图片
- ImageButton背景透明(没有按钮框)以及自定义不同状态样式,自定义背景
- 设置datagridview中button按钮的背景颜色
- 设置背景选择器和button按钮的下划线
- android 设置Button按钮背景透明,半透明
- 设置button没有输入按钮的状态
- 可设置圆角背景边框的按钮, 通过调节色彩明度自动计算按下(pressed)状态颜色
- android 读取sd卡(sdcard)图片设置为按钮背景(Button背景设置)代码
- Python3 Tkinter基础 Button bg 设置按钮的背景颜色
- Android Button 按钮 设置 各种状态 图片 颜色
- 【C#】【WPF】Button按钮动态设置Background背景颜色