Android高手进阶教程(四)之----Android 中自定义属性(attr.xml,TypedArray)的使用! 2010-04-20 21:11:25 标签: xml Android 进阶 attr TypedArray 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。 http://weizhulin.blog.51cto.com/1556324/311453 今天我们的教程是根据前面一节扩展进行的,如果你没有看,请点击 Android高手进阶教程(三) 查看第三课,这样跟容易方便你的理解! 在xml 文件里定义控件的属性,我们已经习惯了android:attrs="" ,那么我们能不能定义自己的属性能,比如:test:attrs="" 呢?答案是肯定的. 好了我就不卖关子了,直接进入主题。大致以下步骤: 一、 在res/values 文件下定义一个attrs.xml 文件.代码如下: view plaincopy to clipboardprint?
一、在res/values文件下定义一个attrs.xml文件.代码如下:
            
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <declare-styleablename="MyView">
  4. <attrname="textColor"format="color"/>
  5. <attrname="textSize"format="dimension"/>
  6. </declare-styleable>
  7. </resources>
  8. 一、在res/values文件下定义一个attrs.xml文件.代码如下:
  9. <?xmlversion="1.0"encoding="utf-8"?>
  10. <resources>
  11. <declare-styleablename="MyView">
  12. <attrname="textColor"format="color"/>
  13. <attrname="textSize"format="dimension"/>
  14. </declare-styleable>
  15. </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() 方法,为了保持以后使用该属性一致性! view plaincopy to clipboardprint?
            
  1. publicMyView(Contextcontext,AttributeSetattrs)
  2. {
  3. super(context,attrs);
  4. mPaint=newPaint();
  5. TypedArraya=context.obtainStyledAttributes(attrs,
  6. R.styleable.MyView);
  7. inttextColor=a.getColor(R.styleable.MyView_textColor,
  8. 0XFFFFFFFF);
  9. floattextSize=a.getDimension(R.styleable.MyView_textSize,36);
  10. mPaint.setTextSize(textSize);
  11. mPaint.setColor(textColor);
  12. a.recycle();
  13. }
  14. publicMyView(Contextcontext,AttributeSetattrs)
  15. {
  16. super(context,attrs);
  17. mPaint=newPaint();
  18. TypedArraya=context.obtainStyledAttributes(attrs,
  19. R.styleable.MyView);
  20. inttextColor=a.getColor(R.styleable.MyView_textColor,
  21. 0XFFFFFFFF);
  22. floattextSize=a.getDimension(R.styleable.MyView_textSize,36);
  23. mPaint.setTextSize(textSize);
  24. mPaint.setColor(textColor);
  25. a.recycle();
  26. }
MyView.java 全部代码如下: view plaincopy to clipboardprint?
            
  1. packagecom.android.tutor;
  2. importandroid.content.Context;
  3. importandroid.content.res.TypedArray;
  4. importandroid.graphics.Canvas;
  5. importandroid.graphics.Color;
  6. importandroid.graphics.Paint;
  7. importandroid.graphics.Rect;
  8. importandroid.graphics.Paint.Style;
  9. importandroid.util.AttributeSet;
  10. importandroid.view.View;
  11. publicclassMyViewextendsView{
  12. privatePaintmPaint;
  13. privateContextmContext;
  14. privatestaticfinalStringmString="WelcometoMrWei'sblog";
  15. publicMyView(Contextcontext){
  16. super(context);
  17. mPaint=newPaint();
  18. }
  19. publicMyView(Contextcontext,AttributeSetattrs)
  20. {
  21. super(context,attrs);
  22. mPaint=newPaint();
  23. TypedArraya=context.obtainStyledAttributes(attrs,
  24. R.styleable.MyView);
  25. inttextColor=a.getColor(R.styleable.MyView_textColor,
  26. 0XFFFFFFFF);
  27. floattextSize=a.getDimension(R.styleable.MyView_textSize,36);
  28. mPaint.setTextSize(textSize);
  29. mPaint.setColor(textColor);
  30. a.recycle();
  31. }
  32. @Override
  33. protectedvoidonDraw(Canvascanvas){
  34. //TODOAuto-generatedmethodstub
  35. super.onDraw(canvas);
  36. //设置填充
  37. mPaint.setStyle(Style.FILL);
  38. //画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标
  39. canvas.drawRect(newRect(10,10,100,100),mPaint);
  40. mPaint.setColor(Color.BLUE);
  41. //绘制文字
  42. canvas.drawText(mString,10,110,mPaint);
  43. }
  44. }
  45. packagecom.android.tutor;
  46. importandroid.content.Context;
  47. importandroid.content.res.TypedArray;
  48. importandroid.graphics.Canvas;
  49. importandroid.graphics.Color;
  50. importandroid.graphics.Paint;
  51. importandroid.graphics.Rect;
  52. importandroid.graphics.Paint.Style;
  53. importandroid.util.AttributeSet;
  54. importandroid.view.View;
  55. publicclassMyViewextendsView{
  56. privatePaintmPaint;
  57. privateContextmContext;
  58. privatestaticfinalStringmString="WelcometoMrWei'sblog";
  59. publicMyView(Contextcontext){
  60. super(context);
  61. mPaint=newPaint();
  62. }
  63. publicMyView(Contextcontext,AttributeSetattrs)
  64. {
  65. super(context,attrs);
  66. mPaint=newPaint();
  67. TypedArraya=context.obtainStyledAttributes(attrs,
  68. R.styleable.MyView);
  69. inttextColor=a.getColor(R.styleable.MyView_textColor,
  70. 0XFFFFFFFF);
  71. floattextSize=a.getDimension(R.styleable.MyView_textSize,36);
  72. mPaint.setTextSize(textSize);
  73. mPaint.setColor(textColor);
  74. a.recycle();
  75. }
  76. @Override
  77. protectedvoidonDraw(Canvascanvas){
  78. //TODOAuto-generatedmethodstub
  79. super.onDraw(canvas);
  80. //设置填充
  81. mPaint.setStyle(Style.FILL);
  82. //画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标
  83. canvas.drawRect(newRect(10,10,100,100),mPaint);
  84. mPaint.setColor(Color.BLUE);
  85. //绘制文字
  86. canvas.drawText(mString,10,110,mPaint);
  87. }
  88. }
三、将我们自定义的MyView 加入布局main.xml 文件中,平且使用自定义属性,自定义属性必须加上: xmlns:test =" http://schemas.android.com/apk/res/com.android.tutor "蓝色 是自定义属性的前缀,红色 是我们包名. main.xml 全部代码如下: view plaincopy to clipboardprint?
<?xml
version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android=" http://schemas.android.com/apk/res/android"

xmlns:test=" http://schemas.android.com/apk/res/com.android.tutor"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<com.android.tutor.MyView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
test:textSize="20px"
test:textColor="#fff"
/>
</LinearLayout>
<?xml
version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android=" http://schemas.android.com/apk/res/android"

xmlns:test=" http://schemas.android.com/apk/res/com.android.tutor"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<com.android.tutor.MyView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
test:textSize="20px"
test:textColor="#fff"
/>
</LinearLayout>
四、运行之效果如下图: 今天就到此结束,大家有什么疑问的,请留言,我会及时答复大家!谢谢~

本文出自 “Android_Tutor” 博客,请务必保留此出处http://weizhulin.blog.51cto.com/1556324/311453

更多相关文章

  1. Android百分比布局:PercentRelativeLayout
  2. Android的多媒体框架OpenCore(PacketVideo)介绍
  3. android使用http协议上传文件
  4. 在android目录下一键生成cscope.out文件
  5. property_get/property_set
  6. Android之Telephony各文件解释
  7. Android常见问题总结(三)
  8. Android(安卓)动画——属性动画Property Animation
  9. android 静音与振动

随机推荐

  1. Android(安卓)贪吃蛇游戏小结
  2. Android之JNI初级篇
  3. 图片的使用
  4. 解决:android:editable is deprecated: Us
  5. Android模拟 HTTP multipart/form-data
  6. Android(安卓)在一个应用中如何启动另外
  7. Android 状态栏透明的一些小结
  8. 详解Android TableLayout中stretchColumn
  9. android面试题大全 android面试题总结
  10. android 动态改变控件位置和大小