一、 在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. FullWebBrowserCookie 取得WebBrowser的完整Cookie

    using System; using System.ComponentModel; using System.Net; using System.Runtime.InteropServices; u ...

  2. Android xUtils3源代码解析之网络模块

    本文已授权微信公众号<非著名程序猿>原创首发,转载请务必注明出处. xUtils3源代码解析系列 一. Android xUtils3源代码解析之网络模块 二. Android xUtil ...

  3. 从tableview中拖动某个精灵

    virtual void registerWithTouchDispatcher(void); virtual bool ccTouchBegan(CCTouch *pTouch,CCEvent *p ...

  4. OpenStack大规模部署详解

    https://blog.csdn.net/karamos/article/details/80130443 0.前言今年的2月22日,OpenStack发布了15个版本Ocata. 走过了7年的发展 ...

  5. C++11 构造函数的改动

    一.继承构造函数 继承构造函数的引入原因:如果基类的构造函数很多,那么子类的构造函数想要实现同样多的构造接口,必须一一调用基类的构造函数,有点麻烦. 于是乎:C++11引入继承构造函数,子类可以通过使 ...

  6. Shell(6): 多线程操作及线程数

    任务需要:当我需要对一个文件夹中的文件,分别压缩的时候: 原始的代码: #!/usr/bin/shell function getdir(){ for element in `ls $1` do #e ...

  7. Python模块学习——tempfile

    主要有以下几个函数: tempfile.TemporaryFile 如何你的应用程序需要一个临时文件来存储数据,但不需要同其他程序共享,那么用TemporaryFile函数创建临时文件是最好的选择.其 ...

  8. 【开发】MFC到Delphi的皮肤移植

       最近一直在学嵌入式,蛋疼啊,专业学软件的去搞硬件原理,真心有点伤不起,比较无聊,希望尽早脱离这个状态. 中午在林同学那里看到他在MFC上应用了Skin++皮肤,这是一款通用的软件换肤套件,支持各 ...

  9. 安装pydiction

    pydiction用来实现代码补全和语法提示功能.pydiction不能通过apt安装,需要自行下载安装. 在GitHub下载源码,可以使用Git迁出到本地,或者下载zip包自行解压,地址为:http ...

  10. 在windows下nginx+django+flup python3

    1.安装python 下载最新的python版本,在本文撰写时为 python 3.4, 下载地址:https://www.python.org/ftp/python/3.4.0/python-3.4 ...