Android之创建自定义属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="customView">
<attr name="android:textColor"/>//在自定义属性中使用Android自带的属性名字
<attr name="customtextSize" format="dimension"/>//自定义属性,format属性表示该属性的单位
</declare-styleable>
</resources>
二、 我们在customView.java 代码修改如下,其中下面的构造方法是重点,我们获取定义的属性R.sytleable.customView_android_textColor和R.sytleable.customView_customtextSize, 获取方法中后面通常设定默认值(float textSize = a.getDimension(R.styleable.customView_customtextSize , 36 ); ), 防止我们在xml 文件中没有定义.从而使用默认值!
获取,customView 就是定义在<declare-styleable name="customView "></declare-styleable> 里的 名字,获取里面属性用 名字_ 属性 连接起来就可以.TypedArray 通常最后调用 .recycle() 方法,为了保持以后使用该属性一致性!
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View; public class customView extends View{
private Paint mPaint;
private static final String mString = "Welcome to Mr Wei's blog";
public customView(Context context) {
super(context);
mPaint = new Paint();
// TODO Auto-generated constructor stub
}
public customView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.customView);
int textColor = a.getColor(R.styleable.customView_android_textColor, 0xff0000);
float textSize = a.getDimension(R.styleable.customView_customtextSize, 36);
mPaint.setColor(textColor);
mPaint.setTextSize(textSize);
a.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
//设置填充
mPaint.setStyle(Style.FILL);
//画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标
canvas.drawRect(new Rect(10, 10, 200, 200), mPaint);
mPaint.setColor(Color.BLUE);
//绘制文字
canvas.drawText(mString, 10, 110, mPaint);
}
}
三、将我们自定义的customView加入布局main.xml 文件中,平且使用自定义属性,自定义属性必须加上:
xmlns:test ="http://schemas.android.com/apk/res/com.lee0000.AutoCustomAttr "蓝色 是自定义属性的前缀,红色 是我们包名.main.xml 全部代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:test="http://schemas.android.com/apk/res/com.lee0000.AutoCustomAttr"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<com.lee0000.AutoCustomAttr.customView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
test:customtextSize="20dp"//自定义属性
android:textColor="#fff">
</com.lee0000.AutoCustomAttr.customView>
</LinearLayout>
上一个demo下载:CustomAttrExample.zip
Android之创建自定义属性的更多相关文章
- Android自定义控件及自定义属性
Android自定义控件及自定义属性 自定义控件 创建自定义控件 自定义一个类,继承View 继承View还是哪个类,取决于你要实现一个什么样的控件 如果你要实现的是一个线性布局的组合控件,就可以继承 ...
- Android自定义控件之自定义属性
前言: 上篇介绍了自定义控件的基本要求以及绘制的基本原理,本篇文章主要介绍如何给自定义控件自定义一些属性.本篇文章将继续以上篇文章自定义圆形百分比为例进行讲解.有关原理知识请参考Android自定义控 ...
- Android 数据库管理— — —创建数据库
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" ...
- [转]Android Studio创建Xposed模块项目时BridgeApi的正确添加方式
使用Android Studio创建的空项目作为Xposed Module App,对于Api Jar包的引用方式,一开始是按照傻瓜式Jar Lib的处理方式,复制XposedBridgeApi-54 ...
- 【Android Studio使用教程2】Android Studio创建项目
创建项目 首先,先指出Android Studio中的两个概念. Project 和 Module .在Android Studio中, Project 的真实含义是工作空间, Module 为一个具 ...
- Android Studio创建项目
创建项目 首先,先指出Android Studio中的两个概念. Project 和 Module .在Android Studio中, Project 的真实含义是工作空间, Module 为一个具 ...
- Android中的自定义属性的实现
Android开发中,如果系统提供的View组件不能满足我们的需求,我们就需要自定义自己的View,此时我们会想可不可以为自定义的View定义属性呢?答案是肯定的.我们可以定义自己的属性,然后像系统属 ...
- Android Studio创建库项目及引用
Android Studio创建库项目其实创建的是在主项目下创建Module模块,这个Module模块创建的时候选择库项目模式. 为什么要这样处理呢?因为在Android Studio中一个WorkS ...
- android 自己创建一个凝视模板
android 自己创建一个凝视模板 作为一名程序猿 不仅要有一个写代码的能力,养成一个良好的编写习惯也是非常重要的. 今天给大家具体介绍一下怎样创建凝视模板,给每一个类和方法都自己手动去凝视信息也 ...
随机推荐
- Intel OIT demo
https://software.intel.com/en-us/blogs/2013/07/18/order-independent-transparency-approximation-with- ...
- 优雅的函数式编程--Clojure概述
欢迎转载,转载请注明出处,徽沪一郎. 楔子 由于阅读storm源码的原因,头一次接触到Clojure.没有花特别的时间来研究clojure语法,只是在一些特殊的用法时,才查了一下clojure官网的文 ...
- 【转】设计模式(九)外观模式Facade(结构型)
设计模式--外观模式Facade(结构型): 1. 概述 外观模式,我们通过外观的包装,使应用程序只能看到外观对象,而不会看到具体的细节对象,这样无疑会降低应用程序的复杂度,并且提高了程序的可维护性. ...
- 【git】删除某个文件的所有历史记录,批量删除远程分支
删除git某个文件的所有历史记录 git的目的就是版本控制,记录每一个版本的变动.然而有的时候我们往往希望从版本库中彻底删除某个文件,不再显示在历史记录中.例如不小心上传了一堆错误的文件,或者不小心上 ...
- Groovy 在eclipse中的使用
今天在写Groovy脚本的时候发现一个问题.如下图,element的elementIterator()方法不能智能显示,只能手动输入.而在eclipse中如果类a没有方法a,那么你是用"cl ...
- 蓝牙的AVCTP协议笔记
1.概述 AVCTP协议描述了蓝牙设备间Audio/Video的控制信号交换的格式和机制,它是一个总体的协议,具体的控制信息由其指定的协议(如AVRCP)实现,AVCTP本身只指定控制comm ...
- 流媒体学习一-------mediastreamer2 的简介
Mediastreamer2 是一个功能强大且小巧的流引擎,专门为音视频电话应用而开发.这个库为linphone中所有的接收.发送多媒体流提供处理,包括音/视频捕获,编码和解码,渲染. 特性: 接收. ...
- Aptana Studio 3的汉化
Aptana Studio 3(下面简称Aptana 3)的汉化方法 1.找到这个网站 http://aptana.com/support 2.单击下面的链接 view documentation 在 ...
- sublime text主要快捷键列表
Ctrl+L 选择整行(按住-继续选择下行)Ctrl+KK 从光标处删除至行尾Ctrl+Shift+K 删除整行Ctrl+Shift+D 复制光标所在整行,插入在该行之前Ctrl+J 合并行(已选择需 ...
- Cure---hdu5879(打表+找规律)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5879 题意:给你一个n ,求∑(1/k2), k from 1 to n; n的范围是不知道的,所以可 ...