说说Android两种为自定义组件添加属性的使用方法和区别解析.doc

说说Android两种为自定义组件添加属性的使用方法和区别解析.doc

  1. 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
说说Android两种为自定义组件添加属性的使用方法和区别解析

说说Android 两种为自定义组件添加属性的使用方法和区别 Android 自定义View 己经不是什么新鲜话题,Android Api提供了一大堆基础组件给我们,需要什么特定功能还需要我们继承它们然后定制更加丰富的功能。前面有篇文章也说过为自定义VIEW添加属性,但只是一笔带过,这里就拿这点来说说吧。 第一种添加属性的方法,之前我也是经常使用这种写法,代码如下: Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeH/--package com.terry.attrs; import android.content.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 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); 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的配置,就可以在我们的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=@string/app_name /com.terry.attrs.EditTextExt1 好了,这是第一种为VIEW注册属性的写法,比较简单就不多介绍。 下面是第二为VIEW注册属性的写法,这里也要重点说说第二种注册 属性的写法和使用要点,先看一下JAVA代码要如何编写: Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeH/--package com.terry.attrs; import android.content.Context; import android.cont

文档评论(0)

4477704 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档