android自定义风格的button

源码下载地址

官方文档来源:http://developer.android.com/guide/topics/ui/controls/button.html

模仿android官方文档,今天学习了自定义button的实现,下面分享给大家。

总结一下,自定义button需要如下几步:

第一步:准备好资源图片,至少2张,需要是.9.png格式的图片。

第二步:将步骤的资源图片拷贝到res/drawable/目录下。

第三步:res/drawable/ 目录下创建xml文件。

第四步:使用步骤3的drawable XML文件。


第一步、第二步省略,直接上图:

  



第三步:res/drawable/ 目录下创建button_custom.xml文件,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_pressed"
          android:state_pressed="true" />
    <item android:drawable="@drawable/button_default" />
</selector>


第四步引用:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="#C1EEEE">


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        //引用此xml文件
        android:background="@drawable/button_custom" 
        android:text="自定义点击效果的按钮" />

</LinearLayout>

效果图如下:

           



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值