Android中实现自定义按钮

项目中有时候考虑到美观,需要自定义Button。

1、在项目的res文件夹中新建文件夹drawable并新建shapes.xml(实现button的外形和颜色的资源)

2、在mainactivity.xml的button属性中设置 android:background="@drawable/shapes"就可以了

圆角按钮、未点击和点击的颜色变化。

效果如下:

shapes.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

     <!-- View点击时的初始化颜色
      gradient渐变
    android:angle  渐变角度,0从上到下,90表示从左到右,数值为45的整数倍默认为0;
    android:startColor  起始颜色
    android:endColor  结束颜色
    android:type  渐变的样式 liner线性渐变 radial环形渐变 sweep
    View的大小 size
    corners  圆角    -->
    <item android:state_pressed="true">
        <shape>
            <gradient android:angle="270" android:endColor="#308014" android:startColor="#308014" />

            <size android:height="60dp" android:width="120dp" />

            <corners android:radius="8dp" />
        </shape>
     </item>
    <!-- View未点击时初始化颜色-->
    <item>
        <shape>
            <gradient android:angle="270" android:endColor="#32CD32" android:startColor="#32CD32" />

            <size android:height="60dp" android:width="120dp" />

            <corners android:radius="8dp" />
        </shape>
   </item>

</selector>

mainactivity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/start"
        android:layout_alignBottom="@+id/start"
        android:layout_marginLeft="30dp"
        android:layout_toRightOf="@+id/start"
        android:text="stop"
android:background="@drawable/shapes"/>

    <Button
        android:id="@+id/unbind"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/bind"
        android:layout_alignBottom="@+id/bind"
        android:layout_alignLeft="@+id/stop"
        android:text="unbind"
        android:background="@drawable/shapes"/>

    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="17dp"
        android:layout_marginTop="108dp"
        android:background="@drawable/shapes"
        android:text="start"
        android:textColor="#FFFFFF" />

    <Button
        android:id="@+id/bind"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/start"
        android:layout_below="@+id/start"
        android:layout_marginTop="43dp"
        android:background="@drawable/shapes"
        android:text="bind" />

</RelativeLayout>
时间: 2024-08-05 19:35:46

Android中实现自定义按钮的相关文章

Android使用selector自定义按钮

可视化编辑器,alt+1到9键,切换到工具区,tab键,选择按钮,esc键,返回编辑 Android使用selector自定义按钮 以前定义按钮的时候,想使用自定义的Button总是习惯在activity中获取该按钮然后,重写该按钮的onTouchListener然后在当中切换按钮的背景图片.这样做不但使activity中的代码变得臃肿而且不能该按钮不能达到重用的目的. 后来接触到了Android 中的selector挺好用的,下面详细讲解一下如何使用selector,首先在res\drawab

Android 中使用自定义字体的方法

1.Android系统默认支持三种字体,分别为:“sans”, “serif”, “monospace 2.在Android中可以引入其他字体 . <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:Android="http://schemas.android.com/apk/res/android" Android:layout_width="fill

Android中制作自定义dialog对话框的实例

http://www.jb51.net/article/83319.htm 这篇文章主要介绍了Android中制作自定义dialog对话框的实例分享,安卓自带的Dialog显然不够用,因而我们要继承Dialog类来制作自己的对话框,需要的朋友可以参考下 自定义dialog基础版很多时候,我们在使用android sdk提供的alerdialog的时候,会因为你的系统的不同而产生不同的效果,就好比如你刷的是MIUI的系统,弹出框都会在顶部显示!这里简单的介绍自定义弹出框的应用. 首先创建布局文件d

浅谈android中的自定义封装易用的Dialog

好久没写android的博客,最近在做一个android的项目,里面用到我们经常用的一个控件就是对话框,大家都知道android自带的对话框是很丑的,android5.x之后除外.所以就出现了自定义view,自己定义美观的对话框.好我们就来自定义对话框. 整体思路:定义一个类然后去继承Dialog类,然后重写相应的构造器方法.大家都知道一般的对话框的创建过程都是来一个AlertDialog.Builder对象,然后使用一些set方法来设置标题内容以及设置一些自定义的view和点击的Button以

Android中的自定义Adapter(继承自BaseAdapter)——与系统Adapter的调用方法一致——含ViewHolder显示效率的优化(转)

Android中很多地方使用的是适配器(Adapter)机制,那我们就要好好把这个Adapter利用起来,并且用出自己的特色,来符合我们自行设计的需要喽~~~ 下面先上一个例子,是使用ViewHolder进行显示效率优化过的工程: package com.test.listviewsimpleadapter;    import java.util.ArrayList;  import java.util.HashMap;  import java.util.List;  import java

Android中View自定义XML属性详解以及R.attr与R.styleable的区别

为View添加自定义XML属性 Android中的各种Widget都提供了很多XML属性,我们可以利用这些XML属性在layout文件中为Widget的属性赋值. 如下所示: <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> 我们可以通过TextView所提供

Android中实现自定义XMPP消息包收发

在Android平台实现XMPP即时通讯主要是使用asmack这个包,asmack是XMPP协议的实现.但是asmack只能帮助我们实现一些基本消息包的收发,如果需要实现特定的自定义消息包收发需要我们自己处理. 一.asmack消息的发送和接收 发送Message消息: 发送一个message结的消息,可以使用sendMessage()发送消息,这个方法有两个重载方法,一种类型的参数是String类型,另一种则是传入Message对象.String类型的方法传入的字符串即为要发送的消息:传入me

ECharts自定义toolbox中增加自定义按钮

今天想能不能在ECharts中的ToolBox增加自己的按钮,然后读了一下ToolBox代码,自己试了试,验证是可以的. 1.效果图 图片中红色框起来的按钮 2.代码 selfButtons:{//自定义按钮 danielinbiti,这里增加,selfbuttons可以随便取名字 show:true,//是否显示 title:'自定义', //鼠标移动上去显示的文字 icon:'test.png', //图标 option:{}, onclick:function(option1) {//点击

Android中使用自定义View实现下载进度的显示

一般有下载功能的应用都会有这样一个场景,需要一个图标来标识不同的状态.之前在公司的项目中写过一个,今天抽空来整理一下. 一般下载都会有这么几种状态:未开始.等待.正在下载.下载结束,当然有时候会有下载出错的状态.等待状态是指用户点击开始下载,但是线程池中没有空闲的线程来处理该次下载,所以状态为等待. 效果图: 这里我只是演示了一下下载和暂停的状态,其他状态没有演示,在代码中设置就可以了. 实现代码: 1.自定义View 1 public class DownloadPercentView ext