简介

总结下之前看的自定义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对应。

实际效果

最新文章

  1. 第一次正式小用Redis存储
  2. 使用C++/C qsort 标准库对结构体进行快速排序
  3. zepto源码--定义变量--学习笔记
  4. 类似github的框架
  5. information_schema中的三个关于锁的表
  6. 并发与同步 (一) ThreadLocal与Synchronized 用哪一个好
  7. Emacs快捷键列表
  8. linux学习方法之六
  9. BZOJ 3674 可持久化并查集加强版 可持久化并查集
  10. .a 文件 和 so 文件
  11. Dubbo+Nacos做注册中心和配置中心
  12. UVa10474
  13. MATLAB 画柱状图(/直方图)修改横坐标名称并使其横着显示
  14. 仿B站项目——(2)环境配置,文件目录
  15. JDK源码之Lock接口
  16. 【洛谷p2142】高精度减法
  17. word_宏示例
  18. 前端之javascript的DOM对象和标签
  19. Spark SQL入门用法与原理分析
  20. leetcode 75. 颜色分类 JAVA

热门文章

  1. (转)Python 字符串
  2. (转) MySQL分区与传统的分库分表
  3. Android IntentFilter匹配规则
  4. PHP CURL 伪造IP和来路
  5. ASP.NET:EntityFramework实现Session
  6. NLP Attention
  7. WPF将TextBox的边框设为圆角的
  8. OOAD之创建型模式之工厂模式
  9. Hadoop+Hive 操作mongodb数据
  10. 软工网络15-Alpha阶段敏捷冲刺