一、 在res/values 文件下定义一个attrs.xml 文件.代码如下:

<?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之创建自定义属性的更多相关文章

  1. Android自定义控件及自定义属性

    Android自定义控件及自定义属性 自定义控件 创建自定义控件 自定义一个类,继承View 继承View还是哪个类,取决于你要实现一个什么样的控件 如果你要实现的是一个线性布局的组合控件,就可以继承 ...

  2. Android自定义控件之自定义属性

    前言: 上篇介绍了自定义控件的基本要求以及绘制的基本原理,本篇文章主要介绍如何给自定义控件自定义一些属性.本篇文章将继续以上篇文章自定义圆形百分比为例进行讲解.有关原理知识请参考Android自定义控 ...

  3. Android 数据库管理— — —创建数据库

    <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" ...

  4. [转]Android Studio创建Xposed模块项目时BridgeApi的正确添加方式

    使用Android Studio创建的空项目作为Xposed Module App,对于Api Jar包的引用方式,一开始是按照傻瓜式Jar Lib的处理方式,复制XposedBridgeApi-54 ...

  5. 【Android Studio使用教程2】Android Studio创建项目

    创建项目 首先,先指出Android Studio中的两个概念. Project 和 Module .在Android Studio中, Project 的真实含义是工作空间, Module 为一个具 ...

  6. Android Studio创建项目

    创建项目 首先,先指出Android Studio中的两个概念. Project 和 Module .在Android Studio中, Project 的真实含义是工作空间, Module 为一个具 ...

  7. Android中的自定义属性的实现

    Android开发中,如果系统提供的View组件不能满足我们的需求,我们就需要自定义自己的View,此时我们会想可不可以为自定义的View定义属性呢?答案是肯定的.我们可以定义自己的属性,然后像系统属 ...

  8. Android Studio创建库项目及引用

    Android Studio创建库项目其实创建的是在主项目下创建Module模块,这个Module模块创建的时候选择库项目模式. 为什么要这样处理呢?因为在Android Studio中一个WorkS ...

  9. android 自己创建一个凝视模板

    android  自己创建一个凝视模板 作为一名程序猿 不仅要有一个写代码的能力,养成一个良好的编写习惯也是非常重要的. 今天给大家具体介绍一下怎样创建凝视模板,给每一个类和方法都自己手动去凝视信息也 ...

随机推荐

  1. 基本Java数据类型

    一.整型:java中的基本数据类型byte,占用1个字节,8位   取值范围:0000 0000 ~ 1111 1111 (-128 ~ 127)   为什么不是:0000 0000 ~ 1111 1 ...

  2. Robocopy

    用法: http://technet.microsoft.com/zh-cn/library/cc733145%28v=ws.10%29.aspx   图形化工具: http://sourceforg ...

  3. dig 命令详解(转载) - 阿权的书房

    在 unix 和 linux 下,建议大家使用 dig 命令来代替 nslookup. dig 命令的功能比 nslookup 强大很多,不像 nslookkup 还得 set 来 set 去的,怪麻 ...

  4. hdu1272 并查集

    如果要输出yes 需要满足 1  这个图连通 2  没有回路 3  0 0 也是yes 看它有没有回路 在un的时候做一次判断就可以了 然后是判断连通 在这里采用的方法是扫一遍 如果这个点出现过就判断 ...

  5. PHP / JavaScript / jQuery 表单验证与处理总结: 第①部分 PHP 表单验证与处理

    PHP VERSION = 5.3.10 一.关于 $_REQUEST PHP 文档关于 $_REQUEST 的说明: 说明 默认情况下包含了 $_GET,$_POST 和 $_COOKIE 的数组. ...

  6. js控制input type=checkbox 的勾选

    <script type="text/javascript">     $(function () {         //双击表格弹出窗口         //为jQ ...

  7. 使用 Vagrant 打造跨平台开发环境

    Vagrant 是一款用来构建虚拟开发环境的工具,非常适合 php/python/ruby/java 这类语言开发 web 应用,“代码在我机子上运行没有问题”这种说辞将成为历史. 我们可以通过 Va ...

  8. perl常用代码

    字符串联结和重复操作符   联接: .  重复:x  联接且赋值(类似+=): .=例:  $newstring = "potato" . "head";  $ ...

  9. 64 位win 7或windows 8下的visual studio不能连接Oracle数据库调试网站的问题

    在64 位win 7或windows 8系统下,visual studio直接F5运行网站调试,你会发现不能连接Oracle数据库,会报一个“ORA-06413: Connection not ope ...

  10. oj 1031 random permutation

    Problem A: Random Permutations Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 91  Solved: 54 Descri ...