今天我们的教程是根据前面一节扩展进行的,如果你没有看,请点击 Android高手进阶教程(三)查看第三课,这样跟容易方便你的理解!
在xml 文件里定义控件的属性,我们已经习惯了android:attrs="" ,那么我们能不能定义自己的属性能,比如:test:attrs="" 呢?答案是肯定的.
好了我就不卖关子了,直接进入主题。大致以下步骤:
一、 在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 全部代码如下:

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

复制代码

三、将我们自定义的MyView 加入布局main.xml 文件中,平且使用自定义属性,自定义属性必须加上:      xmlns:test
="http://schemas.android.com/apk/res/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>

复制代码

四、运行之效果如下图:

最新文章

  1. Python OS模块常用函数说明
  2. sqlserver行列转换问题(网上搜集)
  3. SQL Server常用技巧
  4. 2015安徽省赛 D.锐雯上单不给就送
  5. JAVA Callable
  6. PHP MYSQLI中事务处理
  7. JAVA自定义注释(Target,Retention,Documented,Inherit)
  8. VBS脚本操作网页元素
  9. Windows Server 2012 删除IIS之后 重新启动 桌面不出来 只出现一个命令提示框 解决方法
  10. leetcode 561.Array Partition I-easy
  11. [sed]命令笔记
  12. loj2977 巧克力 (斯坦纳树+随机化)
  13. Django模板语言初识
  14. 接口--Comparable接口【哈夫曼树】
  15. Layui++&gt;&gt;ajax传递数组,防止深度序列化
  16. 实惠VPS推荐
  17. python爬虫快递查询系统(源码)
  18. synchronized使用
  19. TraceLog.cs 累积 C#
  20. mybatis 循环遍历

热门文章

  1. Acrobat 无法在本页面上执行OCR识别
  2. zoj3814
  3. linux常用命令:at 命令
  4. maven intall在target文件夹中自动生成的war包部署服务器时缺斤少两
  5. 彻底明白Flink系统学习5:window、Linux本地安装Flink
  6. jquery中的load方法加载页面无法缓存问题
  7. Linux服务器上Tomcat的Web工程部署
  8. 使用 Vue.js 结合bootstrap 实现的分页控件
  9. 20145332 《网络攻防》 逆向与Bof实验
  10. shell脚本中如何使scp不输入密码即可传输文件