简介

总结下之前看的自定义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. git 忽略某些文件的命令

    正常的,我们在提交项目版本的时候,经常会存在一些需要忽略的文件或者文件夹,那这个时候,我们就可以通过git的一些操作命令来实现! vim .gitignore 通过上面一句话进入编辑忽略文件/文件夹的 ...

  2. 【qboi冲刺NOIP2017复赛试题4】 全套题目+题解+程序

    作为一个好人(验题人),我给大家奉上下这套题的题解,并且预祝大家这套题能够AK: T1题面:Alice现在有n根木棍,他们长度为1,2,3....n,Bob想把某一些木棍去掉,使得Alice剩下的木棍 ...

  3. POJ 1154

    #include<iostream> #include<stdio.h> #define MAXN 20 using namespace std; int DFS(int i, ...

  4. (转)mysql的sql_mode合理设置

    mysql的sql_mode合理设置 目录          http://xstarcd.github.io/wiki/MySQL/MySQL-sql-mode.html http://dev.my ...

  5. iis 如何设置http访问转向https

    把网站设置成https后,发现在浏览器输入域名后,并不能所期望的看到成功访问页面,在输入如:http://www.alipay.com后浏览器自动导航到https://www.alipay.com. ...

  6. 如何在window server IIS上部署可以使用web deploy?

    环境: windows server2012 方式1: 1,下载"wpilauncher.exe" Web平台安装程序.下载地址:http://www.microsoft.com/ ...

  7. 使用fiddler抓手机包遇到问题

    1,http://blog.csdn.net/roland_sun/article/details/30078353 2,参照上面不周,不能实现用自己ip:8888端口下载出证书 解决办法,先从fid ...

  8. Android 开发工具类 05_Logcat 统一管理类

    Logcat 统一管理类: 1.默 认tag 的函数: 2.自定义 tag 的函数. import android.util.Log; // Logcat 统一管理类 public class L { ...

  9. ssh 登录进入 docker container

    1.Container安装ssh服务,博主的linux是centos ① 安装ssh sudo yum install openssh-server #安装ssh服务器 service sshd st ...

  10. Linux-(chgrp,chown,chmod)

    /etc/group Linux /etc/group文件与/etc/passwd和/etc/shadow文件都是有关于系统管理员对用户和用户组管理时相关的文件. Linux /etc/group文件 ...