版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、说说Android 两种为自定义组件添加属性的使用方法和区别Android 自定义View 己经不是什么新鲜话题,Android Api提供了一大堆基础组件给我们,需要什么特定功能还需要我们继承它们然后定制更加丰富的功能。前面有篇文章也说过为自定义VIEW添加属性,但只是一笔带过,这里就拿这点来说说吧。第一种添加属性的方法,之前我也是经常使用这种写法,代码如下:Code highlighting produced by Actipro CodeHighlighter (freeware)http:/www.CodeH com.terry.attrs;import android.content
2、.Context;import android.util.AttributeSet;import android.widget.EditText;import android.widget.LinearLayout;import android.widget.TextView;public class EditTextExt1 extends LinearLayout private String Text = "" public EditTextExt1(Context context) this(context, null); / TODO Auto-generated
3、 constructor stub public EditTextExt1(Context context, AttributeSet attrs) super(context, attrs); / TODO Auto-generated constructor stub int resouceId = -1; TextView tv = new TextView(context); EditText et = new EditText(context); resouceId = attrs.getAttributeResourceValue(null, "Text", 0
4、); if (resouceId > 0) Text = context.getResources().getText(resouceId).toString(); else Text = "" tv.setText(Text); addView(tv); addView(et, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); this.setGravity(LinearLayout.VERTICAL); 这种写法,简单明了,不需要额外XML的配置,
5、就可以在我们的VIEW文件下使用。以上代码通过构造函数中引入的AttributeSet 去查找XML布局的属性名称,然后找到它对应引用的资源ID去找值。使用也时分方便。所以一直以来我也是很喜欢这种写法。如上,自定好VIEW文件就可以在XML布局下如此使用:<com.terry.attrs.EditTextExt1 android:id="+id/ss3" android:layout_width="wrap_content" android:layout_height="wrap_content" Text="stri
6、ng/app_name" ></com.terry.attrs.EditTextExt1>好了,这是第一种为VIEW注册属性的写法,比较简单就不多介绍。下面是第二为VIEW注册属性的写法,这里也要重点说说第二种注册 属性的写法和使用要点,先看一下JAVA代码要如何编写:Code highlighting produced by Actipro CodeHighlighter (freeware)http:/www.CodeH com.terry.attrs;import android.content.Context;import android.content.r
7、es.TypedArray;import android.util.AttributeSet;import android.widget.EditText;import android.widget.LinearLayout;import android.widget.TextView;public class EditTextExt extends LinearLayout public EditTextExt(Context context) this(context, null); / TODO Auto-generated constructor stub public EditTex
8、tExt(Context context, AttributeSet attrs) super(context, attrs); / TODO Auto-generated constructor stub int resouceId = -1; TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.EditTextExt); TextView tv = new TextView(context); EditText et = new EditText(context); int N = typeArr
9、ay.getIndexCount(); for (int i = 0; i < N; i+) int attr = typeArray.getIndex(i); switch (attr) case R.styleable.EditTextExt_Oriental: resouceId = typeArray.getInt(R.styleable.EditTextExt_Oriental,); this.setOrientation(resouceId = 1 ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL); break; case
10、R.styleable.EditTextExt_Text: resouceId = typeArray.getResourceId( R.styleable.EditTextExt_Text, 0); tv.setText(resouceId > 0 ? typeArray.getResources().getText( resouceId) : typeArray .getString(R.styleable.EditTextExt_Text); break; addView(tv); addView(et); typeArray.recycle(); 如上代码,跟前面代码一样。还是用
11、的一个EDITTEXT和TEXTVIEW做基础组件。下面我们一步步分析上面的代码:R.styleable.EditTextExt 代码的是一个attrs指向的一个declare-styleable 的标签,如下代码:Code highlighting produced by Actipro CodeHighlighter (freeware)http:/www.CodeH version="1.0" encoding="UTF-8"?><resources> <declare-styleable name="EditTe
12、xtExt"> <attr name="Text" format="reference|string"></attr> <attr name="Oriental"> <enum name="Horizontal" value="1"></enum> <enum name="Vertical" value="0"></enum> </attr> &
13、lt;/declare-styleable></resources>这个文件位于,values下的attrs.xml目录下面,我比较喜欢一个自定义View 对应一个declare-styleable标签。Tip:一个自定义View 第一部分的代码,TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.EditTextExt);指定为一个declare-styleable,而在declare-styleable 下的attr (即各属性)Android 的ADT 将会自动生成为decla
14、re-styleable的name 名字加上“_”加上对应attr(即属性名称)的名称,如上(EditTextExt_Text)我们要得到Text 就需要R.styleable.EditTextExt_Text,这一点的话可以看看R.java生成文件:Code highlighting produced by Actipro CodeHighlighter (freeware)http:/www.CodeH static final class styleable /* Attributes that can be used with a EditTextExt. <p>Inclu
15、des the following attributes:</p> <table> <colgroup align="left" /> <colgroup align="left" /> <tr><th>Attribute</th><th>Description</th></tr> <tr><td><code>link #EditTextExt_Oriental com.terry.attrs:Ori
16、ental</code></td><td></td></tr> <tr><td><code>link #EditTextExt_Text com.terry.attrs:Text</code></td><td></td></tr> </table> see #EditTextExt_Oriental see #EditTextExt_Text */ public static final int EditTextExt = x7
17、f010000, 0x7f010001 ; /* <p>This symbol is the offset where the link com.terry.attrs.R.attr#Oriental attribute's value can be found in the link #EditTextExt array. <p>Must be one of the following constant values.</p><table><colgroup align="left" /><colg
18、roup align="left" /><colgroup align="left" /><tr><th>Constant</th><th>Value</th><th>Description</th></tr><tr><td><code>Horizontal</code></td><td>1</td><td></td></tr&g
19、t;<tr><td><code>Vertical</code></td><td>0</td><td></td></tr></table> attr name android:Oriental */ public static final int EditTextExt_Oriental = 1; /* <p>This symbol is the offset where the link com.terry.attrs.R.attr#Text attr
20、ibute's value can be found in the link #EditTextExt array. <p>May be a reference to another resource, in the form "<code>+<i>package</i>:<i>type</i>:<i>name</i></code>"or to a theme attribute in the form "<code>?<i>
21、package</i>:<i>type</i>:<i>name</i></code>".<p>May be a string value, using '' to escape characters such as 'n' or 'uxxxx' for a unicode character. attr name android:Text */ public static final int EditTextExt_Text = 0; ;好了,上述的代码
22、写完,我们要在XML布局如何使用呢?这个会跟Android 提供的基础组件的使用方法是一致的。首先,我们要为其提供一个引用包名如下:xmlns:android=" xmlns:terry="上面提供的是android 基础组件的包名,和我们自己组件的包名。写好了包名。就可以像使用andriod 基础组件一样使用了,如下全部XML布局源码:Code highlighting produced by Actipro CodeHighlighter (freeware)http:/www.CodeH version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" xmlns:terry=" android:orientation="vertical" android:la
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 共同富裕目标下农村电商发展模式研究论文
- 2026年林产化学产品制造行业趋势报告及未来五至十年新质生产力与高质量发展
- 2027年信阳市社区卫生服务中心招聘考试护理学考前冲刺试卷及答案
- 2026年网络诊疗面试题及答案
- 2026年体育健康服务行业投资规划建议报告及未来五至十年存量博弈与增量突破
- 2026年试验机制造行业市场调查研究报告及未来五至十年垂直整合与横向拓展
- 2026年11-溴代十一酸行业洞察报告及未来五至十年发展趋势报告
- 2026年印刷专用设备制造行业分析报告及未来五至十年竞争格局分析
- 2026年网络安全事件响应流程实施试题及答案
- 2026年磷酸钙行业洞察报告及未来五至十年发展趋势报告
- DB36-T 2238-2026 高速公路“四改八”改扩建工程交通组织技术指南
- 2025江西省江铜集团招573人笔试历年常考点试题专练附带答案详解
- 水利质保金管理办法
- 大学宿舍楼值班员培训
- JGJ162-2025《建筑施工模板安全技术规范》
- 高考作文审题与立意课件
- 温室大棚项目经济效益和社会效益分析
- 母婴护理服务方案
- 路桥工程检测技术 课件 8 路面外观检测
- TCRIA 30001-2023 橡胶制品工业大气污染防治可行技术指南
- CJT156-2001 沟槽式管接头
评论
0/150
提交评论