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实现动画 ...
随机推荐
- 使用Pycharm 安装三方库
除了使用easy_insatll和pip工具安装Python第三方库外还可以使用pycharm安装Python第三方库,步骤如下: 1.打开pycharm,点击File,再点击settings 2.点 ...
- 简单的 Android 拍照并显示以及获取路径后上传
简单的 Android 拍照并显示以及获取路径后上传 Activity 中的代码,我只贴出重要的事件部分代码 public void doPhoto(View view) { destoryBimap ...
- Windows中 RabbitMQ安装与环境变量配置
RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议.1:安装RabbitMQ需要先安装Erlang语言开发包.下载地址 ht ...
- C#数组和元组
声明数组 int[] myArray; 初始化数组 myArray = new int[4]; 数组是引用类型当初始化完毕后,将在托管堆上分配内存空间,其结构图如下
- hdu_3247_Resource Archiver(AC自动机+bfs+TSP)
题目链接:hdu_3247_Resource Archiver 题意: 有n个资源串,m个病毒串,现在要将所有的资源串整合到一个串内,并且这个串不能包括病毒串,问最短的串长为多少 题解: 将资源串和病 ...
- .Net使用JsonSchema验证Json
最近项目中遇到了这样的需求,需要对上传的Json进行验证,以确保Json数据的准确性.前后使用了两种方式来验证: (1)第一种方式的实现思想:根据Json数据的格式,严格定义相应的类结构,并在Syst ...
- Java中的Unsafe
在阅读AtomicInteger的源码时,看到了这个类:sum.msic.Unsafe,之前从没见过.所以花了点时间google了一下. Unsafe的源码:http://www.docjar.com ...
- LeetCode OJ 55. Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Hexo搭建静态个人博客
Hexo简介 之前在Github上托管的博客就是使用jekyll搭建的,官方的Github Pages同样推荐使用它.我之前体验了一下jekyll,没有达到我想要的效果.于是寻找替代方案,搜索同类博客 ...
- ios书籍推荐
1.Objective-C Programming 内容不多, 却都是精华, 有了一点 C 语言基础可以快速阅读此书, 大概一天时间就可以看完, 看完后对 iOS 开发能够有个基本的印象. 2.iO ...