今天我们的教程是根据前面一节扩展进行的,如果你没有看,请点击 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>
四、运行之效果如下图: 今天就到此结束,大家有什么疑问的,请留言,我会及时答复大家!谢谢~

更多相关文章

  1. Android之LinearLayout线性布局
  2. Android(安卓)JNI 技术简介
  3. android基本理解
  4. 新手学习linux需了解的内容
  5. Android学习系列(一)初识安卓
  6. Eclipse下载Github用Android(安卓)Studio编辑的Android源码
  7. 想抢先体验Android操作系统的魅力吗?那就使用Android(安卓)LiveCD
  8. Google自己出品的dex转jar工具enjarify的介绍
  9. NPM 和webpack 的基础使用

随机推荐

  1. Android(安卓)布局
  2. Android命令大全
  3. android中去掉标题栏和状态栏
  4. android logo:内核、android开机动画
  5. 【Android學習專題】搭建Android(安卓)ND
  6. Android(安卓)5.1源代码与Nexus设备工厂
  7. RatingBar
  8. Edittext 设置
  9. Android抽屉(SlidingDrawer --类似androi
  10. android timed gpio (linux 3.0.0) 受时