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实现动画 ...
随机推荐
- 第四章 android 命名规范和编码规范
书里面讲的比较常见,单个人也是有不同的观点: 因为android绝大部分使用java开发的,因此java相关规范适用于android: Google Style: 英文地址:http://google ...
- SQL learning
一.创建和删除数据库 1.创建用户 //创建用户且置密码,在MySQL中行,但在Oracle中行 ----必须在超级管理员身份下操作 create user hncu identified by ' ...
- bootstrip 折叠菜单
body { font-family: "宋体"; font-size: 12px } .menu { height: 30px; background: #e6e6e6; mar ...
- JS代码备忘
function $(v) { if (typeof v === 'function') { window.onload = v; } else if (typeof v === 'string') ...
- JS复习:第二十章
一.JSON 1.JSON的语法可以表示以下三种类型的值: (1)简单值:字符串.数值.布尔值和null.如:5,“Hello,World!” (2)对象:javascript中对象字面量: var ...
- Java IO 嵌套流、文本的输入输出和存储
Java IO 嵌套流.文本的输入输出和存储 @author ixenos 1. 组合流过滤器(嵌套流) a) 跨平台文件分割符:常量字符串 java.io.File.seperator 等 ...
- java 内省
用内省来操作Bean会给程序带来很大的便利,特别是在很大的实体对象的时候,在其属性很多的时候要为其赋值的时候, package com.bean; public class User { privat ...
- ElasticSearch(1)-入门
下一篇 Elastic Search基础(2) 相关文档: Gitbook[中文未完整]: http://learnes.net/ Gitbook[英文完整]:https://allen8807.gi ...
- Vultr VPS测试IP $5/月KVM-512MB/15G SSD/1T
vultr主要卖点是:SSD硬盘.超低价格.全球13个机房.10Gb服务器带宽.3GHz CPU.性能优异.vultr vps详细评测可参考这篇文章. 以下是vutlr vps官方提供的测试IP地址: ...
- 找斐波那契数列中的第N个数——递归与函数自调用算法
题目描述 Description 用递归的方法求斐波那契数列中的第N个数 输入输出格式 Input/output 输入格式:一行,一个正整数n输出格式: 一行,一个数,表示斐波那契数列中的第N个数 ...