Button按钮状态背景的设置

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


  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值