一、 在res/values 文件下定义一个attrs.xml 文件.代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name="MyView">
  4. <attr name="textColor" format="color" />
  5. <attr name="textSize" format="dimension" />
  6. </declare-styleable>
  7. </resources>

二、 我们在MyView.java 代码编写如下,其中下面的构造方法是重点,我们获取定义的属性R.sytleable.MyView_textColor, 获取方法中后面通常设定默认值(float textSize = a.getDimension(R.styleable.MyView_textSize, 36 ); ) 防止我们在xml 文件中没有定义.从而使用默认值!

MyView 就是定义在<declare-styleable name="MyView "></declare-styleable> 里的 名字,获取里面属性用 名字_ 属性 连接起来就可以.TypedArray 通常最后调用 .recycle() 方法,为了保持以后使用该属性一致性!

  1. public MyView(Context context,AttributeSet attrs)
  2. {
  3. super(context,attrs);
  4. mPaint = new Paint();
  5. TypedArray a = context.obtainStyledAttributes(attrs,
  6. R.styleable.MyView);
  7. int textColor = a.getColor(R.styleable.MyView_textColor,
  8. 0XFFFFFFFF);
  9. float textSize = a.getDimension(R.styleable.MyView_textSize, 36);
  10. mPaint.setTextSize(textSize);
  11. mPaint.setColor(textColor);
  12. a.recycle();
  13. }

MyView.java  MyView控件全部代码如下

  1. public class MyView extends View {
  2. private Paint mPaint;
  3. private Context mContext;
  4. private static final String mString = "Welcome to Mr Wei's blog";
  5. public MyView(Context context) {
  6. super(context);
  7. mPaint = new Paint();
  8. }
  9. public MyView(Context context,AttributeSet attrs)
  10. {
  11. super(context,attrs);
  12. mPaint = new Paint();
  13. TypedArray a = context.obtainStyledAttributes(attrs,
  14. R.styleable.MyView);
  15. int textColor = a.getColor(R.styleable.MyView_textColor,
  16. 0XFFFFFFFF);
  17. float textSize = a.getDimension(R.styleable.MyView_textSize, 36);
  18. mPaint.setTextSize(textSize);
  19. mPaint.setColor(textColor);
  20. a.recycle();
  21. }
  22. @Override
  23. protected void onDraw(Canvas canvas) {
  24. // TODO Auto-generated method stub
  25. super.onDraw(canvas);
  26. //设置填充
  27. mPaint.setStyle(Style.FILL);
  28. //画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标
  29. canvas.drawRect(new Rect(10, 10, 100, 100), mPaint);
  30. mPaint.setColor(Color.BLUE);
  31. //绘制文字
  32. canvas.drawText(mString, 10, 110, mPaint);
  33. }
  34. }

三、将我们自定义的MyView 加入布局main.xml 文件中,并且使用自定义属性,自定义属性必须加上:

xmlns:test ="http://schemas.android.com/apk/res/com.android.tutor"  ,test是自定义属性的前缀,com.android.tutor 是我们包名.

main.xml 全部代码如下:

  1. <?xml
  2. version="1.0" encoding="utf-8"?>
  3. <LinearLayout
  4. xmlns:android="http://schemas.android.com/apk/res/android"
  5. xmlns:test="http://schemas.android.com/apk/res/com.android.tutor"
  6. android:orientation="vertical"
  7. android:layout_width="fill_parent"
  8. android:layout_height="fill_parent"
  9. >
  10. <TextView
  11. android:layout_width="fill_parent"
  12. android:layout_height="wrap_content"
  13. android:text="@string/hello"
  14. />
  15. <com.android.tutor.MyView
  16. android:layout_width="fill_parent"
  17. android:layout_height="fill_parent"
  18. test:textSize="20px"
  19. test:textColor="#fff"
  20. />
  21. </LinearLayout>

format就是格式,里面的就是这个属性对应的格式,下面列出来大致的格式有:

1. reference:参考某一资源ID,以此类推

(1)属性定义:

(2)属性使用:

<imageview< p="">

android:layout_width = "42dip"

android:layout_height = "42dip"

android:background = "@drawable/图片ID"

/>

2. color:颜色值

3. boolean:布尔值

4. dimension:尺寸值。注意,这里如果是dp那就会做像素转换

5. float:浮点值。

6. integer:整型值。

7. string:字符串

8. fraction:百分数。

9. enum:枚举值

10. flag:是自己定义的,类似于 android:gravity="top",就是里面对应了自己的属性值。

11. reference|color:颜色的资源文件。 12.reference|boolean:布尔值的资源文件

注意://由于reference是从资源文件中获取:所以在XML文件中写这个属性的时候必须personattr:name="@string/app_name"这种格式,否则会出错

Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用的更多相关文章

  1. Android中自定义控件TextSize属性问题

    本文主要说明一个自定义控件添加TextSize属性的坑,刚刚从坑里面爬出来,写个随笔,记录一下: *************************************************** ...

  2. Android中的windowSoftInputMode属性详解

    这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下     在前面的一篇文章中 ...

  3. 在Android中把内容写到XML文件中

    在Android中把内容写到XML文件中 saveXmlButton.setOnClickListener(new OnClickListener() { @Override public void ...

  4. 【转】Android中自定义控件的步骤

    原文网址:http://blog.csdn.net/lianchen/article/details/48038969 Android开发中难免遇到需要自定义控件的需求,有些是产品的要求在Androi ...

  5. 《转载-两篇很好的文章整合》Android中自定义控件

    两篇很好的文章,有相互借鉴的地方,整合到一起收藏 分别转载自:http://blog.csdn.net/xu_fu/article/details/7829721 http://www.cnblogs ...

  6. Android中自定义控件,三个构造函数

    自定义控件时,最好抽象得彻底,并且编写需严谨,因为可能程序中多处都会引用到它,或者提供给团队中的其他人使用. 其一般步骤为: 1.创建控件的类文件,定义其功能逻辑.一般继承自现有控件或者View 2. ...

  7. Android中Edittext的属性

    //此为转载别人的,挺不错的 1.EditText输入的文字为密码形式的设置 (1)通过.xml里设置: 把该EditText设为:android:password="true"  ...

  8. android中xmlns:tools属性详解

    今天读到一篇总结的非常棒的文章,写的逻辑很清晰也很实用,很少见到如此棒的文章了.就原文转发过来,我把格式给整理了一下,分享给园子里的各位朋友!好久没写博客了,就为2015年的11月留份纪念吧.希望对你 ...

  9. Android中的ListView属性使用总结

    Android中使用ListView控件比较常见,如果能知道常用的一些属性使用,肯定会少很多坑. 1.ListView是常用的显示控件,默认背景是和系统窗口一样的透明色,如果给ListView加上背景 ...

随机推荐

  1. Merge into 详细介绍

    /*Merge into 详细介绍 MERGE语句是Oracle9i新增的语法,用来合并UPDATE和INSERT语句. 通过MERGE语句,根据一张表或子查询的连接条件对另外一张表进行查询, 连接条 ...

  2. 提取字符串中的数字(C语言)

    题目要求 问题描述:给定一个任意字符串,提取出其中所包含的整数. 样例输入:A12 32bc de51f6576g 样例输出:共计 4 个整数:12 32 51 6576 解决方案-指针版本 核心思想 ...

  3. 数据加密 - TDE透明数据加密原理

    首先需要确定你需要加密的列,Oracle 10g数据库将为包含加密列的表创建一个私密的安全加密密钥(表秘钥), 然后采用你指定的加密算法(AES或3DES)加密指定列的明文数据.此时,保护表的加密密钥 ...

  4. [Windows Azure] Building worker role B (email sender) for the Windows Azure Email Service application - 5 of 5.

    Building worker role B (email sender) for the Windows Azure Email Service application - 5 of 5. This ...

  5. Linux之Kill进程的N种方法

    常规篇: 首先,用ps查看进程,方法如下: $ ps -ef …… smx       1822     1  0 11:38 ?        00:00:49 gnome-terminal smx ...

  6. 每日英语:How Your Knees Can Predict the Weather

    The Wolff family of Paramus, N.J., was eyeing the gathering clouds and debating whether to cancel a ...

  7. ksoap2调用webservice传递参数丢失

    参数有顺序要求,顺序设置正确即可.

  8. filezilla修改默认21端口

    一.filezilla修改端口21 1.修改ftp端口号,例如我们想把21修改成888 2.修改数据端口号为N-1,即888-1=887 3.防火墙中开启端口888和887 完成一个FTP的传输过程不 ...

  9. webpack学习文档

    webpack课程 目录 第1章 webpack简介... 1 1.1    webpack是什么?... 1 1.2    官网地址... 2 1.3    为什么使用 webpack?... 3 ...

  10. python解析XML笔记(etree)

    近期梳理Weblogic数据源,数据源较多,但是每一个数据源在weblogic中是xml方式存在,所以想到批量解析xml,把数据放到数据库后来解决. 需要的数据源信息: WEBLOGIC_HOSTJD ...