已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
今天我们的教程是根据前面一节扩展进行的,如果你没有看,请点击 Android高手进阶教程(三) 查看第三课,这样跟容易方便你的理解!在xml 文件里定义控件的属性,我们已经习惯了android:attrs= ,那么我们能不能定义自己的属性能,比如:test:attrs= 呢?答案是肯定的.好了我就不卖关子了,直接进入主题。大致以下步骤:一、 在res/values 文件下定义一个attrs.xml 文件.代码如下:view plaincopy to clipboardprint?一、在res/values文件下定义一个attrs.xml文件.代码如下: 1 2 3 4 5 6 7 8 一、在res/values文件下定义一个attrs.xml文件.代码如下: 9 10 11 12 13 14 15 二、 我们在MyView.java 代码修改如下,其中下面的构造方法是重点,我们获取定义的属性我们R.sytleable.MyView_textColor, 获取方法中后面通常设定默认值(float textSize = a.getDimension(R.styleable.MyView_textSize, 36 ); ), 防止我们在xml 文件中没有定义.从而使用默认值!获取,MyView 就是定义在 里的名字,获取里面属性用 名字_ 属性 连接起来就可以.TypedArray 通常最后调用 .recycle() 方法,为了保持以后使用该属性一致性!view plaincopy to clipboardprint? 16 public MyView(Context context,AttributeSet attrs) 17 18 super(context,attrs); 19 mPaint = new Paint(); 20 21 TypedArray a = context.obtainStyledAttributes(attrs, 22 R.styleable.MyView); 23 24 int textColor = a.getColor(R.styleable.MyView_textColor, 25 0XFFFFFFFF); 26 float textSize = a.getDimension(R.styleable.MyView_textSize, 36); 27 28 mPaint.setTextSize(textSize); 29 mPaint.setColor(textColor); 30 31 a.recycle(); 32 33 public MyView(Context context,AttributeSet attrs) 34 35 super(context,attrs); 36 mPaint = new Paint(); 37 38 TypedArray a = context.obtainStyledAttributes(attrs, 39 R.styleable.MyView); 40 41 int textColor = a.getColor(R.styleable.MyView_textColor, 42 0XFFFFFFFF); 43 float textSize = a.getDimension(R.styleable.MyView_textSize, 36); 44 45 mPaint.setTextSize(textSize); 46 mPaint.setColor(textColor); 47 48 a.recycle(); 49 MyView.java 全部代码如下:view plaincopy to clipboardprint? 50 package com.android.tutor; 51 import android.content.Context; 52 import android.content.res.TypedArray; 53 import android.graphics.Canvas; 54 import android.graphics.Color; 55 import android.graphics.Paint; 56 import android.graphics.Rect; 57 import android.graphics.Paint.Style; 58 import android.util.AttributeSet; 59 import android.view.View; 60 public class MyView extends View 61 private Paint mPaint; 62 private Context mContext; 63 private static final String mString = Welcome to Mr Weis blog; 64 65 public MyView(Context context) 66 super(context); 67 mPaint = new Paint(); 68 69 public MyView(Context context,AttributeSet attrs) 70 71 super(context,attrs); 72 mPaint = new Paint(); 73 74 TypedArray a = context.obtainStyledAttributes(attrs, 75 R.styleable.MyView); 76 77 int textColor = a.getColor(R.styleable.MyView_textColor, 78 0XFFFFFFFF); 79 float textSize = a.getDimension(R.styleable.MyView_textSize, 36); 80 81 mPaint.setTextSize(textSize); 82 mPaint.setColor(textColor); 83 84 a.recycle(); 85 86 Override 87 protected void onDraw(Canvas canvas) 88 / TODO Auto-generated method stub 89 super.onDraw(canvas); 90 /设置填充 91 mPaint.setStyle(Style.FILL); 92 93 /画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标 94 canvas.drawRect(new Rect(10, 10, 100, 100), mPaint); 95 96 mPaint.setColor(Color.BLUE); 97 /绘制文字 98 canvas.drawText(mString, 10, 110, mPaint); 99 100 101 package com.android.tutor; 102 import android.content.Context; 103 import android.content.res.TypedArray; 104 import android.graphics.Canvas; 105 import android.graphics.Color; 106 import android.graphics.Paint; 107 import android.graphics.Rect; 108 import android.graphics.Paint.Style; 109 import android.util.AttributeSet; 110 import android.view.View; 111 public class MyView extends View 112 private Paint mPaint; 113 private Context mContext; 114 private static final String mString = Welcome to Mr Weis blog; 115 116 public MyView(Context context) 117 super(context); 118 mPaint = new Paint(); 119 120 public MyView(Context context,AttributeSet attrs) 121 122 super(context,attrs); 123 mPaint = new Paint(); 124 125 TypedArray a = context.obtainStyledAttributes(attrs, 126 R.styleable.MyView); 127 128 int textColor = a.getColor(R.styleable.MyView_textColor, 129 0XFFFFFFFF); 130 float textSize = a.getDimension(R.styleable.MyView_textSize, 36); 131 132 mPaint.setTextSize(textSize); 133 mPaint.setColor(textColor); 134 135 a.recycle(); 136 137 Override 138 protected void onDraw(Canvas canvas) 139 / TODO Auto-generated method stub 140 super.onDraw(canvas); 141 /设置填充 142 mPaint.setStyle(Style.FILL); 143 144 /画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标 145 canvas.drawRect(new Rect(10, 10, 100, 100), mPaint); 146 147 mPaint.setColor(Color.BLUE); 148 /绘制文字 149 canvas.drawText(mString, 10, 110, mPaint); 150 151 三、将我们
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 海事法规题库及答案
- 火箭推进题库及答案
- 逻辑学题库及答案
- 盲盒创意美术课件
- 心衰的分析及护理分享
- 河南初三综合试卷及答案
- 养老产业创业机会
- 医学基础知识试题及答案
- 2025年飞行员执照考试《私用驾驶员(直升机)》历年参考题库含答案
- 2025年巷修工高级鉴定试题及答案
- DB33T 2383-2021 公路工程强力搅拌就地固化设计与施工技术规范
- 女生日记杨红樱在线阅读
- 政治-九师联盟2025年高三12月质量检测试题和答案
- GB/T 9755-2024合成树脂乳液墙面涂料
- 市政雨污水管排水工程监理实施细则
- 《工程勘察设计收费标准》(2002年修订本)-完整版-1
- 建筑工地消防安全知识培训
- 《煤矿防治水细则》全文
- 江苏省南通市名校联盟2024~2025学年高三上学期八月模拟演练性月考英语试题英语
- 2023年江苏省高等教育自学考试旅游资源规划与开发试卷3共八套会了这八套通过是没问题的不信试试看
- 2024年山东省高中自主招生数学模拟试卷试题(含答案)
评论
0/150
提交评论