android view构造函数研究
2 public void CustomView(Context context, AttributeSet attrs) {}
3 public void CustomView(Context context, AttributeSet attrs, int defStyle) {}
2 <resources>
3 <style name="customstyle">
4 <item name="android:background">@drawable/bg</item>
5 [... or other style code...]
6 </style>
7 </resources>
2 this(context, attrs, resid);
3 }
2 <resources>
3 <style name="purple">
4 <item name="android:background">#FFFF00FF</item>
5 </style>
6 </resources>
02
03 public class CustomView extends View {
04
05 //C1
06 public CustomView(Context context) {
07 super(context);
08 }
09
10 //C2
11 public CustomView(Context context, AttributeSet attrs) {
12 this(context, attrs, 0);
13 }
14
15 //C3
16 public CustomView(Context context, AttributeSet attrs, int defStyle) {
17 super(context, attrs, defStyle);
18 }
19 }
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:myxmlns="http://schemas.android.com/apk/res/com.your.test"
4 android:orientation="vertical"
5 android:layout_width="fill_parent"
6 android:layout_height="fill_parent" >
7 <com.your.test.CustomView android:layout_width="100px"
8 android:layout_height="100px" />
9 </LinearLayout>
2
3 public class Test extends Activity {
4 @Override
5 public void onCreate(Bundle savedInstanceState) {
6 super.onCreate(savedInstanceState);
7 setContentView(R.layout.main);
8 }
9 }
2 android:layout_height="100px"
3 style="@style/purple"
4 />
2 this(context, attrs, R.style.purple);
3 }
2 <resources>
3 <declare-styleable name="CustomView">
4 <attr name="ourstyle" format="reference" />
5 <attr name="atext" format="string" />
6 </declare-styleable>
7 </resources>
2 android:layout_height="100px"
3 myxmlns:ourstyle="@style/purple"
4 myxmlns:atext="test string"
5 />
2 <item name="ourstyle">@style/purple</item>
3 </style>
2 public CustomView(Context context, AttributeSet attrs) {
3 this(context, attrs, R.attr.ourstyle );
4 }
02 public CustomView(Context context, AttributeSet attrs) {
03 this(context, attrs, attrs.getAttributeNameResource(2));
04 String a1 = ((Integer)R.attr.ourstyle).toString();
05 String a2 = ((Integer)R.styleable.CustomView_ourstyle).toString();
06 String a3 = ((Integer)R.styleable.CustomView[R.styleable.CustomView_ourstyle]).toString();
07 String a4 = ((Integer)R.style.purple).toString();
08 String a5 = ((Integer)attrs.getAttributeNameResource(2)).toString();
09 String a6 = ((Integer)attrs.getAttributeResourceValue(2,0)).toString();
10 String a7 = ((Integer)R.attr.atext).toString();
11 String a8 = ((Integer)R.styleable.CustomView[R.styleable.CustomView_atext]).toString();
12 String a9 = attrs.getAttributeValue(2);
13 String a10 = attrs.getAttributeValue(3);
14 }
02 a2 = 0
03 a3 = 2130771968
04 a4 = 2131034112
05 a5 = 2130771968
06 a6 = 2131034112
07 a7 = 2130771969
08 a8 = 2130771969
09 a9 = @2131034112
10 a10 = test string
2 this(context);
3 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,defStyle, 0);
4 [...other code...]
public TypedArray obtainStyledAttributes (AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)
Return a StyledAttributes holding the attribute values in set that are listed in attrs. In addition, if the given AttributeSet specifies a style class (through the "style" attribute), that style will be applied on top of the base attributes it defines.
Be sure to call StyledAttributes.recycle() when you are done with the array.
When determining the final value of a particular attribute, there are four inputs that come into play:
- Any attribute values in the given AttributeSet.
- The style resource specified in the AttributeSet (named "style").
- The default style specified by defStyleAttr and defStyleRes
- The base values in this theme.
Each of these inputs is considered in-order, with the first listed taking precedence over the following ones. In other words, if in the AttributeSet you have supplied <Button textColor="#ff000000">
, then the button's text will always be black, regardless of what is specified in any of the styles.
Parameters
set | The base set of attribute values. May be null. |
---|---|
attrs | The desired attributes to be retrieved. |
defStyleAttr | An attribute in the current theme that contains a reference to a style resource that supplies defaults values for the StyledAttributes. Can be 0 to not look for defaults. |
defStyleRes | A resource identifier of a style resource that supplies default values for the StyledAttributes, used only if defStyleAttr is 0 or can not be found in the theme. Can be 0 to not look for defaults. |
Returns
- Returns a TypedArray holding an array of the attribute values. Be sure to call
TypedArray.recycle()
when done with it.
android view构造函数研究的更多相关文章
- 简单研究Android View绘制三 布局过程
2015-07-28 17:29:19 这一篇主要看看布局过程 一.布局过程肯定要不可避免的涉及到layout()和onLayout()方法,这两个方法都是定义在View.java中,源码如下: /* ...
- android中自定义view构造函数ContentItemView(Context context, AttributeSet paramAttributeSet)的用处
自己定义一个view <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...
- 简单研究Android View绘制二 LayoutParams
2015-07-28 17:23:20 本篇是关于LayoutParams相关 ViewGroup.LayoutParams文档解释如下: LayoutParams are used by views ...
- 简单研究Android View绘制一 测量过程
2015-07-27 16:52:58 一.如何通过继承ViewGroup来实现自定义View?首先得搞清楚Android时如何绘制View的,参考Android官方文档:How Android Dr ...
- 虾扯蛋:Android View动画 Animation不完全解析
本文结合一些周知的概念和源码片段,对View动画的工作原理进行挖掘和分析.以下不是对源码一丝不苟的分析过程,只是以搞清楚Animation的执行过程.如何被周期性调用为目标粗略分析下相关方法的执行细节 ...
- Android View 事件分发机制 源码解析 (上)
一直想写事件分发机制的文章,不管咋样,也得自己研究下事件分发的源码,写出心得~ 首先我们先写个简单的例子来测试View的事件转发的流程~ 1.案例 为了更好的研究View的事件转发,我们自定以一个My ...
- bug_ _ android.view.InflateException: Binary XML file line #2: Error inflating class <unknown
========= 5.0 android异常“android.view.InflateException: Binary XML file line # : Error inflating ...
- 深入懂得android view 生命周期
作为自定义 view 的基础,如果不了解android view 的生命周期 , 那么你将会在后期的维护中发现这样那样的问题 ....... 做过一段时间android 开发的同学都知道,一般 on ...
- Android物业动画研究(Property Animation)彻底解决具体解释
前p=1959">Android物业动画研究(Property Animation)全然解析具体解释上已经基本展示了属性动画的核心使用方法: ObjectAnimator实现动画 ...
随机推荐
- 跨域资源共享(Cross-Origin Resource Sharing)
目前中文方面的资料还比较少,能搜索到的那仅有的几篇相关介绍,也几乎是雷同的,其中C#方面的更是少之又少. XMLHttpRequest接口是Ajax的根本,而Ajax考虑到安全性的问题,是禁止跨域访问 ...
- Items divided
Items divided 题目链接:http://acm.xidian.edu.cn/problem.php?id=1183 参考:http://www.cnblogs.com/wanghetao/ ...
- ios获取相册图片 压缩图片
从摄像头/相册获取图片 刚刚在上面的知识中提到从摄像头/相册获取图片是面向终端用户的,由用户去浏览并选择图片为程序使用.在这里,我们需要过UIImagePickerController类来和用户交互. ...
- loadrunner 计数器
http://wenku.baidu.com/link?url=oN2kBiABHE1xJmbmZdOmlTCz0sJ8aL3i-hVGiBjAtw-epUW7qrk4f2mAqdOeK5xXw8Sk ...
- POJ 3070 矩阵快速幂解决fib问题
矩阵快速幂:http://www.cnblogs.com/atmacmer/p/5184736.html 题目链接 #include<iostream> #include<cstdi ...
- 导入excel表格的数据--->到mysql中
01下载excel类,将Classes文件夹放入ThinkPHP\Extend\Vendor\位置 下载地址 http://phpexcel.codeplex.com/releases/view/26 ...
- linux screen命令简易使用
在Screen环境下,所有的会话都独立的运行,并拥有各自的编号.输入.输出和窗口缓存,方便在linux系统中后台执行程序. 安装,以centos为例: yum -y install screen 新建 ...
- linux 移除svn文件夹
find . -name .svn -type d -exec rm -fr {} \;
- 正方形网格 TRIANGLE_STRIP连接
unsigned int vIdx = 0, iIdx = 0; for (unsigned int stripRow = 0; stripRow < stripRows; stripRow++ ...
- Integer.valueOf(int)及自动装箱内幕
Integer为什么要提供功能与new Integer(xx)一样的valueOf(xx)方法呢,看了源代码之后,我发现了惊人的内幕. public static Integer valueOf(in ...