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实现动画 ...
随机推荐
- oracle导入dmp数据库文件
要用sys账户登录数据库,创建和dmp文件一样的表空间名称 1. 创建表空间 例如: create tablespace test(表空间名称) datafile 'F:\oracle\oradata ...
- Maze
Maze 题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5094 BFS+状态压缩 把身上携带钥匙的状态压缩成一个2^10的整数.这道题难在 ...
- SPFA算法与dijkstra算法求单源最短路径的比较
SPFA是运用队列,把所有的点遍历到没有能更新的,点可以重复入队 如题http://www.cnblogs.com/Annetree/p/5682306.html dijkstra是每次找出离源点最近 ...
- ajax成功案例
$.ajax({ type:"post", url:"<%=basePath%>getAllVersion", dataType:"jso ...
- CF 602B Approximating a Constant Range
(●'◡'●) #include<iostream> #include<cstdio> #include<cmath> #include<algorithm& ...
- POJ 1230 Pass-Muraille#贪心+vector迭代器用法
(- ̄▽ ̄)-* (注意下面代码中关于iterator的用法,此代码借鉴某大牛) #include<iostream> #include<cstdio> #include< ...
- 文件断点续传原理与实现—— ESFramework 通信框架4.0 进阶(12)
在ESFramework通信框架 4.0 快速上手(13) -- 文件传送,如此简单一文的详细介绍和ESFramework通信框架 4.0 快速上手(14) -- 聊天系统Demo,增加文件传送功能( ...
- 判断括号字符串是否为合法+求n对括号的所有组合
n对括号的有效组合数 参考:https://zh.wikipedia.org/wiki/%E5%8D%A1%E5%A1%94%E5%85%B0%E6%95%B0 import java.util.Ar ...
- vc中主线程等待子线程退出的方法
VC线程同步,在子线程中等待另一子线程结束,通过WaitForSingleObject可以实现,但是如果在主线程中等待子线程结束,这个函数是无法完成要求的,因为它会造成主线程挂起,导致程序死掉.我们可 ...
- C的指针,真的很经典
工作以后,一直使用C++,也做过Objective C,各种类的方法封装得很好,使用很简单,今天偶尔翻看一下 严蔚敏 的 <数据结构>,第一个程序demo就看了半天,一是由于demo的变量 ...