1.attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyView">
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
</declare-styleable>
</resources>

2.在构造方法中使用

public MyView(Context context,AttributeSet attrs){
super(context,attrs);
mPaint = new Paint(); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyView); int textColor = a.getColor(R.styleable.MyView_textColor, 0XFFFFFFFF); //怎么获取:名字_ 属性 连接起来就可以
float textSize = a.getDimension(R.styleable.MyView_textSize, 36); mPaint.setTextSize(textSize);
mPaint.setColor(textColor); a.recycle();
}

3.在布局文件中使用

<?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.android.tutor" //自定义属性前缀 与 包名
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <com.android.tutor.MyView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
test:textSize="20px"
test:textColor="#fff"/>
</LinearLayout>

Android自定义属性:attr.xml 与 TypedArray的更多相关文章

  1. android开发:Android 中自定义属性(attr.xml,TypedArray)的使用

    今天我们的教程是根据前面一节扩展进行的,如果你没有看,请点击 Android高手进阶教程(三)查看第三课,这样跟容易方便你的理解! 在xml 文件里定义控件的属性,我们已经习惯了android:att ...

  2. Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用

    一. 在res/values 文件下定义一个attrs.xml 文件.代码如下: <?xml version="1.0" encoding="utf-8" ...

  3. Android中自定义属性attr.xml的格式详解

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

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

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

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

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

  6. Android自定义属性时TypedArray的使用方法

    有时候android传统的页面布局不足以满足我们的需求,常常需要自己定义view,通常继承View,然后重写构造方法以及onDraw等函数,再 具体实现自己定义的复杂view.我们知道在给控件赋属性时 ...

  7. Android 自定义View二(深入了解自定义属性attrs.xml)

    1.为什么要自定义属性 要使用属性,首先这个属性应该存在,所以如果我们要使用自己的属性,必须要先把他定义出来才能使用.但我们平时在写布局文件的时候好像没有自己定义属性,但我们照样可以用很多属性,这是为 ...

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

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

  9. Android自定义属性

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

随机推荐

  1. PHP学习笔记(6)js点击刷新验证码

    用“换一张”来控制验证码刷新,js脚本怎么写 宏朋雄 | 浏览 3663 次  2012-06-11 22:41 2012-06-12 01:49   最佳答案   <img src=“验证码文 ...

  2. RxBinding -- 官网说明

    RxBinding -- 官网说明 新建 模板 小书匠 作用 组件 平台绑定 support-v4 绑定 appcompact-v7 绑定 design 库绑定 recyclerview-v7 绑定 ...

  3. centos单机安装Hadoop2.6

    一,安装环境 硬件:虚拟机 操作系统:Centos 6.4 64位 IP:10.51.121.10 主机名:datanode-4 安装用户:root 二,安装JDK 安装JDK1.6或者以上版本.这里 ...

  4. Unix系统编程(四)creat系统调用

    我好疑惑啊,creat系统调用为啥没有以e结尾呢?搞得我每次都怀疑我敲错了. 在早期的UNIX实现中,open只有两个参数,无法创建新文件,而是使用creat系统调用创建并打开一个新文件. int c ...

  5. Intellij IDEA常用快捷键整理

    ps:使用Intellij IDEA编辑器有蛮长一段时间了.哈哈.整合一下常用的快捷.避免自己以后还要再找. 顺序大概就是从上到下的 Alt + ENTER : 自动修正,提示Alt + Insert ...

  6. Servlet 容器对URI的处理

    问题 请求到达server以后,server是怎样处理URI请求资源路径的,在与web.xml文件里的映射进行比对时的原则是什么. 方案 针对精确匹配.通配符匹配.后缀匹配三种模式改动web.xml文 ...

  7. Enable Authentication on MongoDB

    1.Connect to the server using the mongo shell mongo mongodb://localhost:27017 2.Create the user admi ...

  8. 重写(Override)

    重写(Override) 重写是子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变.即外壳不变,核心重写! 重写的好处在于子类可以根据需要,定义特定于自己的行为. 也就是说子类 ...

  9. asp.net 读取配置文件方法

    方法1: System.Collections.Specialized.NameValueCollection nvc = (System.Collections.Specialized.NameVa ...

  10. poj 3414(简单bfs)

    题目链接:http://poj.org/problem?id=3414 思路:bfs简单应用,增对瓶A或者瓶B进行分析就可以了,一共6种状态. #include<iostream> #in ...