找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 11313|回复: 0
打印 上一主题 下一主题
收起左侧

关于在AndroidStudio中自定义Button的样式

[复制链接]
跳转到指定楼层
楼主
ID:223768 发表于 2017-8-1 10:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
于自定义Button的样式,是在res/drawable文件夹下自定义一个xml文件,在xml文件中进行定义,然后通过将Button的backgroud改为定义的xml文件实现的。下面将进行详细介绍,并举例说明。


在res/drawable下新建一个button_style.xml文件,这个文件用于描述按钮的样式

[html] view plain copy



  • <?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/btn_p"/>  
  •     <item android:state_pressed="false" android:drawable="@drawable/btn_n"/>  
  • </selector>

有很多样式可够选择:

在使用时,

  • <?xml version="1.0" encoding="utf-8"?>  
  • <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  •     android:layout_width="fill_parent"  
  •     android:layout_height="fill_parent"  
  •     android:orientation="vertical" >  
  •   
  •     <TextView  
  •         android:layout_width="fill_parent"  
  •         android:layout_height="wrap_content"  
  •         android:text="@string/hello" />  
  •   
  •     <Button  
  •         android:id="@+id/button1"  
  •         android:layout_width="wrap_content"  
  •         android:layout_height="wrap_content"  
  •         android:background="@drawable/button_style"  
  •         />  
  •   
  • </LinearLayout>
这样就完成了简单的Button样式。
可以利用上述的参数设置完成一些很酷炫的按钮样式。

[html] view plain copy


  • <?xml version="1.0" encoding="utf-8"?>  
  • <selector  
  •     xmlns:android="http://schemas.android.com/apk/res/android">  
  •     <item android:state_pressed="true">  
  •         <shape>  
  •             <gradient android:startColor="#99CC33"  
  •                 android:endColor="#99CC33"  
  •                 android:angle="270" />  
  •             <stroke android:width="1dip" android:color="#f403c9" />  
  •             <corners android:radius="2dp" />  
  •             <padding android:left="10dp" android:top="10dp"  
  •                 android:right="10dp" android:bottom="10dp" />  
  •         </shape>  
  •     </item>  
  •   
  •     <item android:state_focused="true">  
  •         <shape>  
  •             <gradient android:startColor="#ffc2b7"  
  •                 android:endColor="#ffc2b7"  
  •                 android:angle="270" />  
  •             <stroke android:width="1dip" android:color="#f403c9" />  
  •             <corners android:radius="2dp" />  
  •             <padding android:left="10dp" android:top="10dp"  
  •                 android:right="10dp" android:bottom="10dp" />  
  •         </shape>  
  •     </item>  
  •   
  •     <item>  
  •         <shape>  
  •             <gradient android:startColor="#336633"  
  •                 android:endColor="#336633"  
  •                 android:angle="180" />  
  •             <stroke android:width="1dip" android:color="#f403c9" />  
  •             <corners android:radius="5dip" />  
  •             <padding android:left="10dp" android:top="10dp"  
  •                 android:right="10dp" android:bottom="10dp" />  
  •         </shape>  
  •     </item>  
  • </selector>  
  • 感谢大家观看!


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩1
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表