简介

总结下之前看的自定义View的内容,结合一个简单的例子,阐述下基本用法和大致的使用流程,这个例子比较简单,更复杂的自定义View,随着自己的学习,后面再慢慢添加。作为一个Android开发者,这部分应该是不可或缺的。

自定义属性

位置:res/values/attrs.xml

格式:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="name_of_style">
<attr name="name_of_attr" format="reference|string|color|boolean|dimension|enum|flag|float|fraction|integer"/>
</declare-styleable>
</resources>
format 意义
reference 参考某一资源ID, 如R.drawable.xxx
string 字符串
color 颜色
boolean 布尔值
dimension 尺寸值
enum 枚举值,例如
flag 位或运算,例如:
float 浮点数
fraction 百分数,例如pivotX,pivotY这一类属性
integer 整数

获取自定义属性

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.name_of_style);

Color mColor = typedArray.getColor(R.styleable.name_of_style_name_of_attr, Color.BLUE);//其他的属性获取类似
typedArray.recycle();//记得回收

名字:

R.styleable.{name_of_style}

R.styleable.{name_of_style}_{name_of_attr}

举个栗子

举个栗子,实现一个背景为渐变色的圆角按钮,圆角半径,开始颜色,中心颜色,结束颜色,渐变方向用户可自定义。

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CornerButton">
<attr name="corner_radius" format="dimension" />
<attr name="background_start_color" format="color" />
<attr name="background_center_color" format="color" />
<attr name="background_end_color" format="color" />
<attr name="backgrouund_gradient_orientation">
<enum name="TOP_BOTTOM" value="0" />
<enum name="TR_BL" value="1" />
<enum name="RIGHT_LEFT" value="2" />
<enum name="BR_TL" value="3" />
<enum name="BOTTOM_TOP" value="4" />
<enum name="BL_TR" value="5" />
<enum name="LEFT_RIGHT" value="6" />
<enum name="TL_BR" value="7" />
</attr>
</declare-styleable>
</resources>

CornerButton

public class CornerButton extends Button {

    private GradientDrawable mBg;
private float mRandius;
private int mStartColor;
private int mCenterColor;
private int mEndColor;
private int mOrientation; public CornerButton(Context context, AttributeSet attrs) {
super(context, attrs); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CornerButton);
mRandius = typedArray.getDimension(R.styleable.CornerButton_corner_radius, 10);
mStartColor = typedArray.getColor(R.styleable.CornerButton_background_start_color, Color.BLUE);
mCenterColor = typedArray.getColor(R.styleable.CornerButton_background_center_color, Color.GREEN);
mEndColor = typedArray.getColor(R.styleable.CornerButton_background_end_color, Color.BLACK);
mOrientation = typedArray.getInt(R.styleable.CornerButton_backgrouund_gradient_orientation, 0);
typedArray.recycle(); int[] colors = {mStartColor, mCenterColor, mEndColor};
mBg = new GradientDrawable();
mBg.setCornerRadius(mRandius);
mBg.setOrientation(Orientation.TR_BL);
mBg.setColors(colors);
switch (mOrientation) {
case 0:
mBg.setOrientation(Orientation.TOP_BOTTOM);
break;
case 1:
mBg.setOrientation(Orientation.TR_BL);
break;
case 2:
mBg.setOrientation(Orientation.RIGHT_LEFT);
break;
case 3:
mBg.setOrientation(Orientation.BR_TL);
break;
case 4:
mBg.setOrientation(Orientation.BOTTOM_TOP);
break;
case 5:
mBg.setOrientation(Orientation.BL_TR);
break;
case 6:
mBg.setOrientation(Orientation.LEFT_RIGHT);
break;
case 7:
mBg.setOrientation(Orientation.TL_BR);
break;
}
this.setBackground(mBg);
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yidong="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="gs.com.customview.MainActivity"> <gs.com.customview.CornerButton
android:id="@+id/cb_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
yidong:background_start_color="#CCFF0000"
yidong:background_center_color="#CCAADD00"
yidong:background_end_color="#CC00EEFF"
yidong:corner_radius="100dp"
yidong:backgrouund_gradient_orientation="BOTTOM_TOP"
/>
</RelativeLayout>

xmlns:yidong=”http://schemas.android.com/apk/res-auto”



yidong:corner_radius=”100dp”

XML命名空间和属性的Tag对应。

实际效果

<Android 基础(二十六)> 渐变色圆角Button的更多相关文章

  1. Bootstrap <基础二十六>进度条

    Bootstrap 进度条.在本教程中,你将看到如何使用 Bootstrap 创建加载.重定向或动作状态的进度条. Bootstrap 进度条使用 CSS3 过渡和动画来获得该效果.Internet ...

  2. <Android 基础(十六)> Toast

    介绍 A toast provides simple feedback about an operation in a small popup. It only fills the amount of ...

  3. Android进阶(二十六)MenuInflater实现菜单添加

    MenuInflater实现菜单添加 前言 之前实现的Android项目中可以实现菜单的显示.但是再次调试项目时发现此功能已无法实现,很是令人费解.难道是因为自己手机Android系统的问题?尝试通过 ...

  4. Bootstrap <基础二十二>超大屏幕(Jumbotron)

    Bootstrap 支持的另一个特性,超大屏幕(Jumbotron).顾名思义该组件可以增加标题的大小,并为登陆页面内容添加更多的外边距(margin).使用超大屏幕(Jumbotron)的步骤如下: ...

  5. Bootstrap <基础二十五>警告(Alerts)

    警告(Alerts)以及 Bootstrap 所提供的用于警告的 class.警告(Alerts)向用户提供了一种定义消息样式的方式.它们为典型的用户操作提供了上下文信息反馈. 您可以为警告框添加一个 ...

  6. Bootstrap<基础二十四> 缩略图

    Bootstrap 缩略图.大多数站点都需要在网格中布局图像.视频.文本等.Bootstrap 通过缩略图为此提供了一种简便的方式.使用 Bootstrap 创建缩略图的步骤如下: 在图像周围添加带有 ...

  7. Bootstrap <基础二十九>面板(Panels)

    Bootstrap 面板(Panels).面板组件用于把 DOM 组件插入到一个盒子中.创建一个基本的面板,只需要向 <div> 元素添加 class .panel 和 class .pa ...

  8. Bootstrap <基础二十八>列表组

    列表组.列表组件用于以列表形式呈现复杂的和自定义的内容.创建一个基本的列表组的步骤如下: 向元素 <ul> 添加 class .list-group. 向 <li> 添加 cl ...

  9. Bootstrap<基础二十> 标签

    Bootstrap 标签.标签可用于计数.提示或页面上其他的标记显示.使用 class .label 来显示标签,如下面的实例所示: <!DOCTYPE html> <html> ...

  10. Android进阶(二十八)上下文菜单ContextMenu使用案例

    上下文菜单ContextMenu使用案例 前言 回顾之前的应用程序,发现之前创建的选项菜单无法显示了.按照正常逻辑来说,左图中在"商品信息"一栏中应该存在选项菜单,用户可进行分享等 ...

随机推荐

  1. PostgreSQL 数据库错误代码解释

    PostgreSQL 服务器发出的所有消息都赋予了五个字符 的错误代码, 这些代码遵循 SQL 的 "SQLSTATE" 代码的习惯.需要知道发生了什么错误条件的应用通常应该测试错 ...

  2. C# 中out,ref,params参数的使用

    C#中有三个高级参数,分别是out,ref,params:   1.out参数 方法使用return 只能返回一个值(一个数值或一个指针值),out参数可以帮助我们在一个方法中返回多个值,不限类型. ...

  3. SQL Server 数据库存储过程实例

    USE [UFDATA_999_2014] GO /****** Object: StoredProcedure [dbo].[p_XMonPerNums] Script Date: 06/12/20 ...

  4. tcp ip三次握手链接和四次挥手断开

      先来个整体的流程图       一 三次握手目的是为了建立连接... 1 核心的就是client端和service端,进行数据"报文" 交换 2 报文,目的是互相通知,确认链接 ...

  5. (转)支持Multi Range Read索引优化

    支持Multi Range Read索引优化 原文:http://book.51cto.com/art/201701/529465.htm http://book.51cto.com/art/2016 ...

  6. (转)linux如何让历史记录不记录敏感命令

    有时候为了服务器安全,防止别人窥探我们输入的命令,我们可以清空历史记录,而更多的时候,我们选择的是在输入特殊命令时候,强制历史记录不记住该命令.实验方法:先执行export HISTCONTROL=i ...

  7. 搜集C++实现的线程池

    现在很多语言都内置了线程池实现,但C++中却没有.本文列举一些C++实现的线程池工具. Boost.Threadpool 项目首页:http://threadpool.sourceforge.net ...

  8. certificate verify fails (https://gems.ruby-china.org错误

    首先:执行这一步报错的背景是: 更换gems源, 通常执行 gem sources --add https://gems.ruby-china.org/ --remove https://rubyge ...

  9. phpdocumentor生成代码注释文档(linux)

    1,默认安装lnmp环境(php7),默认pear安装 2,   pear channel-discover pear.phpdoc.org pear install phpdoc/phpDocume ...

  10. haml参考大全

    原文来自: http://blackanger.blog.51cto.com/140924/47642   Haml是一种用来描述任何XHTML web document的标记语言,它是干净,简单的. ...