当我们自定义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);
} }

我是天王盖地虎的分割线

最新文章

  1. HTML5新特性——HTML 5 Canvas vs. SVG
  2. Daily Scrum Meeting ——FirstDay
  3. js实现复制到剪贴板功能,兼容所有浏览器
  4. Python:Sqlmap源码精读之解析xml
  5. ArcGIS Engine要素渲染和专题图制作(转)
  6. InteractivePNG
  7. WPF 之 资源(Resource)
  8. C# 使用winForm的TreeView显示中国城镇四级联动
  9. table转list
  10. SQL三大范式
  11. DirectSound应用
  12. [ios2]苹果iOS 5限制应用本地存储问题 【转】
  13. 解决mac os下mcss命令报错:env: node\r: No such file or directory
  14. Python——day14 三目运算、推导式、递归、匿名、内置函数
  15. Linux常见问题整理
  16. Python自动化开发 - select模块
  17. 【基于初学者的SSH】struts02 数据封装的三种方式详解
  18. 12-5 张雨RTCM3数据解码解不出的原因
  19. HTML5背景音乐的暂停与播放
  20. 栈的理解和代码实现(java)

热门文章

  1. Token以及签名signature的设计与实现
  2. How Visual Studio 2012 Avoids Prompts for Source
  3. USB with NXP Microcontrollers
  4. 项目内部IT/电商/信息化类简报,分享电子版
  5. c# 主机和网络字节序的转换 关于网络字节序和主机字节序的转换
  6. utf-8与utf-8(无BOM)的区别
  7. ArcGIS 在高清屏中主界面界面字体和图标显示过小,如何解决?
  8. Jquery 验证 validate
  9. MEF在运行时替换插件
  10. sharepoint 2013 网站集解锁