Android -- TypedArray
当我们自定义View的时候,在给View赋值一些长度宽度的时候,一般都是在layout布局文件中进行的。,比如android:layout_height="wrap_content",除此之外,我们也可以自己定义属性,这样在使用的时候我们就可以使用形如 myapp:myTextSize="20sp"的方式了。
values/attrs.xml
首先要创建变量,创建了个values/attrs.xml文件,或文件名任意,但是要在values目录下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyView">
<attr name="textSize" format="dimension" />
</declare-styleable>
</resources>
其中resource是跟标签,可以在里面定义若干个declare-styleable,<declare-styleable name="MyView">中name定义了变量的名称,下面可以再自定义多个属性,针对<attr name="textSize" format="dimension"/>来说,其属性的名称为"textSize",format指定了该属性类型为dimension,只能表示字体的大小。
- format还可以指定其他的类型比如:
- reference 表示引用,参考某一资源ID
- string 表示字符串
- color 表示颜色值
- dimension 表示尺寸值
- boolean 表示布尔值
- integer 表示整型值
- float 表示浮点值
- fraction 表示百分数
- enum 表示枚举值
- flag 表示位运算
layout
<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" > <us.yydcdut.MyView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
myapp:textSize="20sp"
myapp:myColor="#324243"/> </LinearLayout>
可以看到多了xmlns:myapp="http://schemas.android.com/apk/res/com.eyu.attrtextdemo" ,以及在自定义View中 myapp:textSize="20sp" ,myapp:myColor="#324243"
obtainStyledAttributes
context通过调用obtainStyledAttributes方法来获取一个TypeArray,然后由该TypeArray来对属性进行设置
obtainStyledAttributes方法有三个,我们最常用的是有一个参数的obtainStyledAttributes(int[] attrs),其参数直接styleable中获得
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);
调用结束后务必调用recycle()方法,否则这次的设定会对下次的使用造成影响
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);
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setStyle(Style.FILL);
canvas.drawText("aaaaaaa", 10, 50, paint);
} }
我是天王盖地虎的分割线
Android -- TypedArray的更多相关文章
- Android中自定义属性(attrs.xml,TypedArray的使用)
做Android布局是件很享受的事,这得益于他良好的xml方式.使用xml可以快速有效的为软件定义界面.可是有时候我们总感觉官方定义的一些基本组件不够用,自定义组件就不可避免了.那么如何才能做到像官方 ...
- Android-深入理解android自定义属性(AttributeSet,TypedArray)
属性 自定义属性,首先要定义出来属性,我们新建一个attrs.xml: <?xml version="1.0" encoding="utf-8"?> ...
- Android自定义属性时TypedArray的使用方法
有时候android传统的页面布局不足以满足我们的需求,常常需要自己定义view,通常继承View,然后重写构造方法以及onDraw等函数,再 具体实现自己定义的复杂view.我们知道在给控件赋属性时 ...
- Android 自定义属性(attrs.xml,TypedArray)
做Android布局是件很享受的事,这得益于他良好的xml方式.使用xml可以快速有效的为软件定义界面.可是有时候我们总感觉官方定义的一些基本组 件不够用,自定义组件就不可避免了.那么如何才能做到像官 ...
- Android开发学习之TypedArray类
在学习Android的开发中,学习Gallery视图显示图片的过程中,在设置图片适配器的时候,用到了此TypedArray类型,这次根据android SDK,一块把此类型弄清楚! android.c ...
- 【Android】TypedArray和obtainStyledAttributes使用
在编写Android自定义按钮示例基础上,如果要指定字体大小产生这样的效果: 其实是不需要自定义变量的,可以直接使用TextView的配置属性: <com.easymorse.textbutto ...
- Android - 自定义控件和属性(attr和TypedArray)
http://blog.csdn.net/zjh_1110120/article/details/50976027 1.attr format 取值类型 以ShapeView 为例 <decla ...
- Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用
一. 在res/values 文件下定义一个attrs.xml 文件.代码如下: <?xml version="1.0" encoding="utf-8" ...
- android开发:Android 中自定义属性(attr.xml,TypedArray)的使用
今天我们的教程是根据前面一节扩展进行的,如果你没有看,请点击 Android高手进阶教程(三)查看第三课,这样跟容易方便你的理解! 在xml 文件里定义控件的属性,我们已经习惯了android:att ...
随机推荐
- FireDAC 下的 Sqlite [12] - 备忘录(草草结束这个话题了)
该话题的继续延伸主要就是 SQL 的语法了, 草草收场的原因是现在的脑筋已经进入了 IntraWeb 的世界. 相关备忘会随时补充在下面: //连接多个数据库的参考代码: FDConnection1. ...
- web网页上面调用qq
<a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=2812415198&site=q ...
- STM32F4 Timer External Clock TI2 Both Edges Demo
#define CLK_FREQ ( 10000 ) #define CORE_FREQ ( 168000000 ) static void TIM_GPIO_Config( void ) { GPI ...
- User Agent Strings列表 — Kejun's Blog
曾经,ME 就需要这里的东西了. http://www.useragentstring.com/pages/All/
- delphi 取得任意程序的命令行
program GetCommandLineExDemo; uses Windows; constSystemHandleInformation = 16;ProcessBasicInformatio ...
- IIS 调用Microsoft.Office.Interop.Word.Documents.Open 返回为null
控制面板->管理工具->组件服务->计算机->我的电脑->DCom配置->找到Microsoft Word文档 之后 单击属性打开此应用程序的属性对话框. 2. 单 ...
- Java Dictionary 类存储键值
字典(Dictionary) 字典(Dictionary) 类是一个抽象类,它定义了键映射到值的数据结构. 当你想要通过特定的键而不是整数索引来访问数据的时候,这时候应该使用Dictionary. 当 ...
- url提交参数类
url提交参数类 type /// <summary> /// 准备url /// </summary> TynUrl = class private FUrl, FComma ...
- iOS6新特征:UICollectionView介绍-非常棒 -转
传送门:http://www.devdiv.com/forum.php?mod=viewthread&tid=128378
- 通俗易懂地讲解 __block 变量
__block 变量 一般来说,在block内只能读取在同一个作用域的变数而且没有办法修改在block外定义的任何变量,此时若我们想要这些变数能够在block中被修改,就必须在前面挂上__block的 ...