1)自定义button样式

一、采用图片方式

首先新建Android XML文件,类型选Drawable,根结点选selector,自定义一个文件名。

随后,开发环境自动在新建的文件里加了selector结点,我们只需要在selector结点里写上三种状态时显示的背景图片(按下、获取焦点,正常)即可。具体如下:

<?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/play_press" ;/>
<item android:state_focused="true" android:drawable="@drawable/play_press" ;/>
<item android:drawable="@drawable/play" ;/>
</selector>

注:这里获取焦点跟点击时显示的是同一张图片,必须严格照上面的顺序写,不可倒。
  最后,只要在布局时写Button控件时应用到Button的Background属性即可,如:

<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_style">
</Button>

二、采用自定义方式

在源代码中,只需要修改button_style文件,同样三种状态分开定义:

<?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="#0d76e1"
android:endColor="#0d76e1"
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="#000000" android:endColor="#ffffff"
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>

注:代码中的各属性含义为:

gradient 主体渐变

startColor开始颜色,endColor结束颜色 ,

angle开始渐变的角度(值只能为90的倍数,0时为左到右渐变,90时为下到上渐变,依次逆时针类推)
    stroke 边框 width 边框宽度,color 边框颜色
    corners 圆角 radius 半径,0为直角
    padding text值的相对位置

2)自定义style样式

一、在style.xml中自定义样式

以自定义text文本大小和颜色为例,自定义一个名称为"testStyle"的style代码如下:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
</style> <style name="testStyle">
<item name="android:textSize">30px</item>
<item name="android:textColor">#1110CC</item>
<item name="android:width">150dip</item>
<item name="android:height">150dip</item>
</style>
</resources>

二、在layout文件中引用自定义的"testStyle"的style样式

<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"
tools:context=".MainActivity" > <TextView
style="@style/testStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" /> </RelativeLayout>

从以上代码可以看出,应用方式为@style/testStyle。

android中样式和自定义button样式的更多相关文章

  1. 如何使用XE2及更高版本中提供的自定义皮肤(样式)功能

    源:如何使用XE2及更高版本中提供的自定义皮肤(样式)功能 1. 制作样式文件: 点击 XE2+ 的 IDE 菜单上的 Tools-->Bitmap Style Designer, 打开设计器. ...

  2. Android中如何做到自定义的广播只能有指定的app接收

    今天没吊事,又去面试了,具体哪家公司就不说了,因为我在之前的blog中注明了那些家公司的名字,结果人家给我私信说我泄露他们的题目,好吧,我错了...其实当我们已经在工作的时候,我们可以在空闲的时间去面 ...

  3. android中添加只有border-left的样式

    如何在android中的边框添加只有左边边框有颜色的样式呢 1. 相应的drawable文件 <?xml version="1.0" encoding="utf-8 ...

  4. Android中GridView通过自定义适配器(未优化)实现图文视图排列

    Android中GridView组件用来以网格方式排列视图,与矩阵类似,当屏幕上有很多元素(文字.图片或其他元素)需要显示时,可以使用该组件.下面我们通过代码实现如下图例(为了方便截图,将事件处理(土 ...

  5. Android中快速实现自定义字体!

    前言:我们都知道,Android中默认的字体是黑体,而大多数app也都是使用的这种字体,但我们发现,大多数app中,个别地方字体非常好看,例如app的标题栏,菜单栏等地方,那他们是怎么做到的呢?有两种 ...

  6. 030 Android 第三方开源下拉框:NiceSpinner的使用+自定义Button样式+shape绘制控件背景图+图片选择器(selector)

    1.NiceSpinner下拉框控件介绍 Android原生的下拉框Spinner基本上可以满足Android开发对于下拉选项的设计需求,但现在越来越流行的下拉框不满足于Android原生提供的下拉框 ...

  7. 微信小程序之分享或转发功能(自定义button样式)

    小程序页面内发起转发 通过给 button 组件设置属性open-type="share",可以在用户点击按钮后触发 Page.onShareAppMessage 事件,如果当前页 ...

  8. WPF自定义Button样式(按钮长度随Content长度自适应)

    代码如下: <Style x:Key="ButtonStyle" TargetType="Button"> <Setter Property= ...

  9. 小程序里button边框有黑线解决办法(自定义button样式)

    .go_to_user::after{ border:1px solid transparent; } button的class为go_to_user button{ padding:; box-si ...

随机推荐

  1. 常用函数和STL

    #include <bits/stdc++.h> using namespace std; #define PI acos(-1.0) int main() { printf(" ...

  2. null相关

    本文转自:http://www.cnblogs.com/peida/archive/2013/06/14/Guava_Optional.html null代表不确定的对象: Java中,null是一个 ...

  3. T2821 天使之城 codevs

    http://codevs.cn/problem/2821/ 题目描述 Description 天使城有一个火车站,每辆火车都从A方向驶入车站,再从B方向驶出车站. 为了调度火车,火车站设有停放轨道, ...

  4. URAL - 1860 Fiborial

    Discription Consider a sequence F i that satisfies the following conditions:  Find the number of dif ...

  5. java常用组件

    一.Jpanel 1.面板:容器类组件 2.用途:与Layout配合使用,JFrame—>JPanel—>Layout 二.JTextField 1.文本框 2.JPasswordFiel ...

  6. Java创建和解析Json数据方法(五)——Google Gson包的使用

    (五)Google Gson包的使用 1.简介 Gson包中,使用最多的是Gson类的toJson()和fromJson()方法:         ①toJson():将java对象转化为json数据 ...

  7. jquery 查找子元素的几种方法

    <div class="tm-clear tb-hidden tm_brandAttr" id="J_BrandAttr" style="dis ...

  8. 基于HTML5的PACS--HTML5图像处理

    在此之前,此系统是结合DICOM的WADO标准,在浏览器里通过javascript操作返回的JPG图片.这种服务器端解析,客户端展现的方式,对实现图像的移动.缩放.旋转.测量等图像操作能够实现实时的交 ...

  9. Python基础语法01

    Python 标识符 在python里,标识符有字母.数字.下划线组成. 在python中,所有标识符可以包括英文.数字以及下划线(_),但不能以数字开头. python中的标识符是区分大小写的. 以 ...

  10. vu 是什么

    unsigned char 是无符号字符型 volatile      易变,易失的 volatile  unsigned char i; 意思是定义一个无符号字符型的变量 i. 这个变量存放在内存中 ...