android TypedArray
2016-03-05 14:18
423 查看
位置:frameworks/base/core/java/android/content/res/TypedArray.java
Container for an array of values that were retrieved with
{@link Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int)}
or {@link Resources#obtainAttributes}
翻译过来就是一组数据的集合,通过obtainStyledAttributes(AttributeSet, int[], int, int)、和obtainStyledAttributes(AttributeSet, int[], int, int)可以获取
使用:
(1)在项目文件res/value下面创建一个attr.xml文件,该文件中包含若干个attr集合,例如
其中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的代码中引入自定义属性,修改构造函数context通过调用obtainStyledAttributes方法来获取一个TypeArray,然后由该TypeArray来对属性进行设属性obtainStyledAttributes方法有三个,我们最常用的是有一个参数的obtainStyledAttributes(int[] attrs),其参数直接styleable中获得TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);
调用结束后务必调用recycle()方法,否则这次的设定会对下次的使用造成影响
Container for an array of values that were retrieved with
{@link Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int)}
or {@link Resources#obtainAttributes}
翻译过来就是一组数据的集合,通过obtainStyledAttributes(AttributeSet, int[], int, int)、和obtainStyledAttributes(AttributeSet, int[], int, int)可以获取
使用:
(1)在项目文件res/value下面创建一个attr.xml文件,该文件中包含若干个attr集合,例如
<?xml version="1.0" encoding="utf-8"?> <resources xmlns:android="http://schemas.android.com/apk/res/android"> <declare-styleable name="CalculatorEditText"> <attr name="minTextSize" format="dimension" /> <attr name="maxTextSize" format="dimension" /> <attr name="stepTextSize" format="dimension" /> <attr name="myTextSize" format="dimension" /> </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的代码中引入自定义属性,修改构造函数context通过调用obtainStyledAttributes方法来获取一个TypeArray,然后由该TypeArray来对属性进行设属性obtainStyledAttributes方法有三个,我们最常用的是有一个参数的obtainStyledAttributes(int[] attrs),其参数直接styleable中获得TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);
调用结束后务必调用recycle()方法,否则这次的设定会对下次的使用造成影响
</pre><pre name="code" class="java"> final TypedArray a = context.obtainStyledAttributes( attrs, R.styleable.CalculatorEditText, defStyle, 0); mMaximumTextSize = a.getDimension( R.styleable.CalculatorEditText_maxTextSize, getTextSize()); mMinimumTextSize = a.getDimension( R.styleable.CalculatorEditText_minTextSize, getTextSize()); mStepTextSize = a.getDimension(R.styleable.CalculatorEditText_stepTextSize, (mMaximumTextSize - mMinimumTextSize) / 3); a.recycle();(3)在使用到该自定义view的布局文件中键入如下的一行:绿色是自己定义属性的前缀名字,粉色是项目的包名,这样一来,在我们自己定义的view的属性中,就可以使用自己在attr中定义的属性。
相关文章推荐
- Android开发之自定义控件
- Android:Activity中onCreate方法的参数及用途
- Android之Design Support Library整理
- android 系统应用在运行时被卸载
- Android SQLite的使用
- Best practices in Android development
- Android中的HandlerThread和IntentService
- android编译与反编译
- Android 将assets中的文件拷贝到本地SD卡使用
- Android开发学习之路--【前言】
- Android开发之蓝牙开发
- Android获取代码Color资源
- android开发之路05
- Android为RecyclerView添加头部底部的简单方法
- Android 利用shape自定义进度条样式
- 《Android开发艺术探索》读后笔记1
- Android的DisplayUtil
- Android之Notification的多种用法
- android开发系列之gradle认识
- 第二周笔记