一、 在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. 【iCore3双核心板】扩展引脚分布

    PDF 版下载: http://files.cnblogs.com/files/xiaomagee/iCore3%E6%89%A9%E5%B1%95%E5%BC%95%E8%84%9A%E5%88%8 ...

  2. axure rp extension的下载

    使用chrome无法直接打开axure制作的prd文档,需要下载相关插件.平时用的shadowsocksFQ软件,登录chrome的网上应用商店,确发现无法正常下载, 于是通过其他途径下载了改插件,拖 ...

  3. [学点英语]扎克伯格给女儿的信,translation of zucherber's letter to her daughter( Chinese version)

    A letter to our daughter 扎克伯格写给女儿的信   Mark Zuckerberg·Tuesday, December 1, 2015 Dear Max, 亲爱的玛克斯 You ...

  4. DS实验题 word

    题目: 代码: // // main.cpp // word // // Created by wasdns on 16/11/11. // Copyright © 2016年 wasdns. All ...

  5. mysql执行完select后,释放游标

    内存释放 在我们执行完SELECT语句后,释放游标内存是一个很好的习惯. .可以通过PHP函数mysql_free_result()来实现内存的释放. 以下实例演示了该函数的使用方法. 2.mysql ...

  6. 挑战python

    00 热身 http://www.pythonchallenge.com/pc/def/0.html import math print math.pow(2,38); # 274877906944 ...

  7. Connection Management and Security

    High Performance My SQL  THIRD EDITION Each client connection gets its own thread within the server ...

  8. Java Tool

    PS参数详解 http://blog.csdn.net/hanner_cheung/article/details/6081440 JVM 参数 JVM调优总结 -Xms -Xmx -Xmn –Xss ...

  9. Apache Kafka源码分析 - PartitionStateMachine

    startup 在onControllerFailover中被调用, initializePartitionState private def initializePartitionState() { ...

  10. Delphi 200X、XE中如何用并行实现循环的计算

    interface uses Classes, SysUtils; type TParallelProc = reference to procedure(i: Integer; ThreadID: ...