当前位置:操作系统 > 安卓/Android >>

Android 自定义Button按钮显示样式(正常、按下、获取焦点)

现在的用户对APP的外观看得很重要,如果APP内所有元件都用Android默认样式写,估计下面评论里就有一堆在骂UI丑的。今天学习自定义Button按钮样式。Button样式修改的是Button的背景(Background)属性。
首先写一个定义Button样式的XML文件:
新建Android XML文件,类型选Drawable,根结点选selector,文件名就buton_style吧。
程序自动给我们刚刚建的文件里加了selector结点,我们只需要在selector结点里写上三种状态时显示的背景图片(按下、获取焦点,正常)。
代码如下:
 
 
 
<span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(0,153,0)"><span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(0,0,0)"><strong><?xml</strong></span> <span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(0,0,102)">version</span>=<span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(255,0,0)">"1.0"</span> <span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(0,0,102)">encoding</span>=<span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(255,0,0)">"utf-8"</span><span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(0,0,0)"><strong>?></strong></span></span>  
<span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(0,153,0)"><span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(0,0,0)"><strong><selector</strong></span> <span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(0,0,102)">xmlns:android</span>=<span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(255,0,0)">"http://schemas.android.com/apk/res/android"</span><span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(0,0,0)"><strong>></strong></span></span>  
    <span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(0,153,0)"><span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(0,0,0)"><strong><item</strong></span> <span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(0,0,102)">android:state_pressed</span>=<span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(255,0,0)">"true"</span> <span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(0,0,102)">android:drawable</span>=<span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(255,0,0)">"@drawable/play_press"</span> <span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(0,0,0)"><strong>/></strong></span></span>  
    <span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(0,153,0)"><span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(0,0,0)"><strong><item</strong></span> <span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(0,0,102)">android:state_focused</span>=<span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(255,0,0)">"true"</span> <span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(0,0,102)">android:drawable</span>=<span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(255,0,0)">"@drawable/play_press"</span> <span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(0,0,0)"><strong>/></strong></span></span>  
    <span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(0,153,0)"><span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(0,0,0)"><strong><item</strong></span> <span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(0,0,102)">android:drawable</span>=<span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(255,0,0)">"@drawable/play"</span> <span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(0,0,0)"><strong>/></strong></span></span>  
<span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(0,153,0)"><span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(0,0,0)"><strong></selector<span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline">></span></strong></span></span>  

 

 
我这里获取焦点跟点击时显示的是同一张图片,必须严格照上面的顺序写,不可倒。
接下来只要在布局时写Button控件时应用到Button的Background属性即可。
 
<span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(0,153,0)"><span style="border:0px; font-family:inherit; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(0,0,0)"><strong><Button</strong></span> <span style="border
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,