有时候android传统的页面布局不足以满足我们的需求,常常需要自己定义view,通常继承View,然后重写构造方法以及onDraw等函数,再 具体实现自己定义的复杂view。我们知道在给控件赋属性时,通常使用的是android系统自带的属性,比如 android:layout_height="wrap_content",除此之外,我们亦可以自己定义属性,这样在使用的时候我们就可以使用形如 myapp:myTextSize="20sp"的方式了,步骤大致如下:

1》在项目文件res/value下面创建一个attr.xml文件,该文件中包含若干个attr集合,例如:
<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <declare-styleable name="MyView">  
        <attr name="myTextSize" format="dimension"/>  
        <attr name="myColor" format="color"/>  
    </declare-styleable>  
</resources>  
 
其中resource是跟标签,可以在里面定义若干个declare-styleable,<declare-styleable name="MyView">中name定义了变量的名称,下面可以再自定义多个属性,针对<attr name="myTextSize" format="dimension"/>来说,其属性的名称为"myTextSize",format指定了该属性类型为dimension,只 能表示字体的大小。
format还可以指定其他的类型比如;
reference   表示引用,参考某一资源ID
string   表示字符串
color   表示颜色值
dimension   表示尺寸值
boolean   表示布尔值
integer   表示整型值
float   表示浮点值
fraction   表示百分数
enum   表示枚举值
flag   表示位运算
 
2》在使用到该自定义view的布局文件中键入如下的一行:
绿色是自己定义属性的前缀名字,粉色是项目的包名,这样一来,在我们自己定义的view的属性中,就可以使用自己在attr中定义的属性啦,例如:
[html] 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:myapp="http://schemas.android.com/apk/res/com.eyu.attrtextdemo"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical"  
    tools:context=".MainActivity" >  
  
    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="@string/hello_world" />  
    <com.eyu.attrtextdemo.MyView  
        android:layout_height="wrap_content"  
        android:layout_width="wrap_content"  
        myapp:myTextSize="20sp"  
        myapp:myColor="#324243"/>  
  
</LinearLayout>  
 
 
3》在自定义view的代码中引入自定义属性,修改构造函数
context通过调用obtainStyledAttributes方法来获取一个TypeArray,然后由该TypeArray来对属性进行设置
obtainStyledAttributes方法有三个,我们最常用的是有一个参数的obtainStyledAttributes(int[] attrs),其参数直接styleable中获得
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);
调用结束后务必调用recycle()方法,否则这次的设定会对下次的使用造成影响  
具体如下:
[java] 
package com.eyu.attrtextdemo;  
  
import android.content.Context;  
import android.content.res.TypedArray;  
import android.graphics.Canvas;  
import android.graphics.Paint;  
import android.graphics.Paint.Style;  
import android.util.AttributeSet;  
import android.view.View;  
  
public class MyView extends View{  
    public Paint paint;  
  
    public MyView(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        paint = new Paint();  
          
        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);      
        int textColor = a.getColor(R.styleable.MyView_myColor, 003344);  
        float textSize = a.getDimension(R.styleable.MyView_myTextSize, 33);  
        paint.setTextSize(textSize);  
        paint.setColor(textColor);  
        a.recycle();  
    }  
  
    public MyView(Context context) {  
        super(context);  
        // TODO Auto-generated constructor stub  
    }  
      
    @Override   www.2cto.com
    protected void onDraw(Canvas canvas) {  
        // TODO Auto-generated method stub  
        super.onDraw(canvas);     
        paint.setStyle(Style.FILL);  
        canvas.drawText("aaaaaaa", 10, 50, paint);  
    }  
      
      
      
}  
 
运行后:
                                        

Android自定义属性时TypedArray的使用方法的更多相关文章

  1. (转)Android自定义属性时format选项( <attr format="reference" name="background" /> )

    Android自定义属性时format选项可以取用的值   1. reference:参考某一资源ID. (1)属性定义: [html] view plaincopyprint? <declar ...

  2. Android-深入理解android自定义属性(AttributeSet,TypedArray)

    属性 自定义属性,首先要定义出来属性,我们新建一个attrs.xml: <?xml version="1.0" encoding="utf-8"?> ...

  3. android.content.res.TypedArray-深入理解android自定义属性(AttributeSet,TypedArray)

    属性 自定义属性,首先要定义出来属性,我们新建一个attrs.xml: <?xml version="1.0" encoding="utf-8"?> ...

  4. Android自定义属性时format选项可以取用的值

    1. reference:参考某一资源ID. (1)属性定义: <declare-styleable name="名称"> <attr format=" ...

  5. Android 打包时 Keep 住某些方法或类

    # ${android_sdk}/tools/proguard/proguard-android.txt # Understand the @Keep support annotation. -kee ...

  6. Android 自定义属性(attrs.xml,TypedArray)

    做Android布局是件很享受的事,这得益于他良好的xml方式.使用xml可以快速有效的为软件定义界面.可是有时候我们总感觉官方定义的一些基本组 件不够用,自定义组件就不可避免了.那么如何才能做到像官 ...

  7. Android退出时关闭所有Activity的方法

    Android退出时,有的Activity可能没有被关闭.为了在Android退出时关闭所有的Activity,设计了以下的类: //关闭Activity的类 public class CloseAc ...

  8. 退出Android程序时清除所有activity的实现方法

    思路: 1. 自定义ActivityList管理类,添加删除维护该list; 2.Activity Stack 类似上面: 3.singleTask定义一个Activity为该启动模式,然后当返回时, ...

  9. Android自定义属性

    上一篇讲解了Android自定义View,这篇来讲解一下Android自定义属性的使用,让你get新技能.希望我的分享能帮助到大家. 做Android布局是件很享受的事,这得益于他良好的xml方式.使 ...

随机推荐

  1. 单片微机原理P0:80C51结构原理

    本来我真的不想让51的东西出现在我的博客上的,因为51这种东西真的太low了,学了最多就所谓的垃圾科创利用一下,但是想一下这门课我也要考试,还是写一点东西顺便放博客上吧. 这一系列主要参考<单片 ...

  2. 第 2 章 代理模式【Proxy Pattern】

    第 2 章 代理模式[Proxy Pattern] 以下内容出自:24种设计模式介绍与6大设计原则.pdf 什么是代理模式呢?我很忙,忙的没空理你,那你要找我呢就先找我的代理人吧,那代理人总要知道被代 ...

  3. PowerDesigner从SqlServer 数据库中导入实体模型

    此篇是之前写的,从我的CSDN博客挖过来的- 一.开启数据库服务并配置ODBC数据源 1.开启数据库服务 (1)通过SQL Server Configuration Manager配置工具启动SQL ...

  4. [python]用profile协助程序性能优化

    转自:http://blog.csdn.net/gzlaiyonghao/article/details/1483728 本文最初发表于恋花蝶的博客http://blog.csdn.net/lanph ...

  5. 李洪强iOS开发之-环信02.2_环信官网下载环信 SDK

    李洪强iOS开发之-环信02.2_环信官网下载环信 SDK 移动客服即时通讯云 iOS SDK 当前版本:V3.1.4 2016-07-08 [ 版本历史 ] | 开发指南 | 知识库 | Demo源 ...

  6. ScrollView中嵌套两个ListView

    做的项目中要使用两个ListView在同一个页面上下显示,因为数据源不同,不能通过在Adapter中设置标志位去区分显示,最后只能硬着头皮做一个ScrollView嵌套两个ListView,但按正常情 ...

  7. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: SELECT command denied to user’

    Linux环境 Mysql+Hibernate command denied to user 错误 错误信息 如下: com.mysql.jdbc.exceptions.jdbc4.MySQLSynt ...

  8. 抽象工厂模式[wiki]

        抽象工厂[编辑] 维基百科,自由的百科全书   跳转至: 导航. 搜索   以统一塑模语言中的类型图来表示抽象工厂 抽象工厂模式(英语:Abstract factory pattern)是一种 ...

  9. 在ubuntu12.04下编译android4.1.2添加JNI层出现问题

    tiny4412学习者,在ubuntu12.04下编译android4.1.2添加JNI层出现问题: (虚心请教解决方法) trouble writing output: Too many metho ...

  10. BZOJ3132: 上帝造题的七分钟

    3132: 上帝造题的七分钟 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 483  Solved: 222[Submit][Status] Desc ...