系统控件view有三个构造方法,分别如下

public DrawView(Context context) {
super(context); } public DrawView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr); } public DrawView(Context context, AttributeSet attrs) {
super(context, attrs); }

关于构造方法,网上给的解释是

如果在Code中实例化一个View会调用第一个构造函数,如果在xml中定义会调用第二个构造函数,而第三个函数系统是不调用的,要由 View(我们自定义的或系统预定义的View,如此处的CustomTextView和Button)显式调用,比如在这里我们在第二个构造函数中调用 了第三个构造函数,并将R.attr.CustomizeStyle传给了第三个参数。第三个参数的意义就如同它的名字所说的,是默认的Style,只是这里没有说清楚,这里的默认的Style是指它在当前Application或Activity所用的Theme中的默认Style。

至于只有一个参数的方法,我们主要是在new一个view的时候,会使用。它的那个参数是上下文环境。如

Button button = new Button(this);

至于两个参数跟三个参数的方法调用情况,网上有说是跟你在xml中是否使用了style有关。不过,我验证了下,无论有无style,都仅仅是调用两个参数的构造方法。验证如下

自定义style

package com.example.testcode;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View; public class DrawView extends View { public DrawView(Context context) {
super(context); } public DrawView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr); } public DrawView(Context context, AttributeSet attrs) {
super(context, attrs); } @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub //这两个方法必须有一个,否则会报错
//super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(200, 200);
} @Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
// TODO Auto-generated method stub
Log.e("123", "change===" + changed + " left===" + left + " top==="
+ top + " right===" + right + " bottom===" + bottom);
super.onLayout(changed, left, top, right, bottom);
} @Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
Log.e("123", "onDraw");
super.onDraw(canvas);
}
}

xml中的使用

1.当没有style的时候

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.testcode.MainActivity" > <com.example.testcode.DrawView
android:layout_width="100dp"
android:layout_height="100dp"
/> </RelativeLayout>

打印结果

- ::13.445: E/(): drawview_2

2.当有style的时候

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.testcode.MainActivity" > <com.example.testcode.DrawView
style="@style/test"
/> </RelativeLayout>

style

<style name="test">
<item name="android:layout_width">100dp</item>
<item name="android:layout_height">100dp</item>
<item name="android:background">#ff0000</item>
</style>

打印结果

- ::13.445: E/(): drawview_2

所以,xml中的控件,只调用两个参数的方法。而三个参数的方法,其实是在二个参数的方法中显式调用的。我们可以看下Button源码

public class Button extends TextView {
public Button(Context context) {
this(context, null);
} public Button(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.buttonStyle);
} public Button(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
} public Button(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
} @Override
public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
super.onInitializeAccessibilityEvent(event);
event.setClassName(Button.class.getName());
} @Override
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfo(info);
info.setClassName(Button.class.getName());
}
}

从上面我们很清除的看到了这几个构造方法的调用关系

1.1参数->2参数(没有传入属性)->3参数(传入了系统默认的style)->4参数

xml中控件调用构造方法的更多相关文章

  1. Layout.xml中控件的ID命名方式

    控件 缩写 LayoutView lv RelativeView rv TextView tv Button btn ImageButton imgBtn ImageView mgView 或则 iv ...

  2. 设置xml中控件的圆润边框效果

    <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...

  3. duilib中控件拖拽功能的实现方法(附源码)

    转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/41144283 duilib库中原本没有显示的对控件增加拖拽的功能,而实际 ...

  4. 安卓常用的xml控件配件的使用包含shape,declare-styleable、selector

    今天就讲我所遇到的常用到的一些通过xml文件制作的背景效果,以后用到的话就直接使用啦!哈哈,我一向就是这么懒! 接下来,就开始介绍了 1.shape的使用,可以混合使用 xml控件配置属性 andro ...

  5. 【VS开发】VS2010 MFC中控件、对话框等背景颜色动态修改的方法

    [VS开发]VS2010 MFC中控件.对话框等背景颜色动态修改的方法 标签(空格分隔):[VS开发] 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 说明: ...

  6. C#中控件数组的讨论

    VB用得习惯后,到C#中来觉得很奇怪,如此好的控件数组怎么不见了.“众所周知,控件数组最主要的两个优点:可以循环附值:可以响应同一个事件.从而大大简化了代码.引自http://wenku.baidu. ...

  7. WPF 中获取DataGrid 模板列中控件的对像

    WPF 中获取DataGrid 模板列中控件的对像 #region 当前选定行的TextBox获得焦点 /// <summary> /// 当前选定行的TextBox获得焦点 /// &l ...

  8. Metro中控件WebView访问外部的网页显示一片空白

    Metro中控件WebView访问外部的网页显示一片空白 解决方案: ​下载安装了Initex.Software.Proxifier.v3.21.Standard.Edition.Incl.Keyma ...

  9. form表单中控件较多,加载完成后切换页面都很慢的解决方法

    form表单中控件较多,加载完成后点击都很慢,为什么?我一页面中form表单里面上百个控件(如input.select.radio.checkbox等),还有一些js脚本,加载速度还可以,都能全部显示 ...

随机推荐

  1. BZOJ 3143 高斯消元+贪心....

    思路: 先算一下每条边经过次数的期望 转化为每个点经过次数的期望 边的期望=端点的期望/度数 统计一下度数 然后高斯消元 贪心附边权--. //By SiriusRen #include <cm ...

  2. Java中MySQL事务处理举例

    实例(以sql语句中的insert语句为例) import java.sql.Connection; import java.sql.DriverManager; import java.sql.Pr ...

  3. 08:Challenge 1

    总时间限制:  10000ms 单个测试点时间限制:  1000ms 内存限制:  262144kB 描述 给一个长为N的数列,有M次操作,每次操作是以下两种之一: (1)修改数列中的一个数 (2)求 ...

  4. Android-加载大图,照片墙的实现

    照片墙这种功能现在应该算是挺常见了,在很多应用中你都可以经常看到照片墙的身影.它的设计思路其实也非常简单,用一个GridView控件当作“墙”,然后随着GridView的滚动将一张张照片贴在“墙”上, ...

  5. 51Nod 1010 只包含因子2 3 5的数(打表+二分)

    K的因子中只包含2 3 5.满足条件的前10个数是:2,3,4,5,6,8,9,10,12,15. 所有这样的K组成了一个序列S,现在给出一个数n,求S中 >= 给定数的最小的数. 例如:n = ...

  6. 仿函数(functor)

    仿函数(functor),就是使一个类的使用看上去像一个函数.其实现就是类中实现一个operator(),这个类就有了类似函数的行为,就是一个仿函数类了. In computer programmin ...

  7. 01背包-第k优解

    The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup&quo ...

  8. php八大设计模式之策略模式

    策略模式提供一个虚拟的整体,根据不同的要求(参数)提供不同的"零件"(调用不同的"零件"实现不同的结果). <?php /** * 策略模式 * 跟工厂模 ...

  9. Redis序列化存储Java集合List等自定义类型

    在"Redis学习总结和相关资料"http://blog.csdn.net/fansunion/article/details/49278209 这篇文章中,对Redis做了总体的 ...

  10. 题解 P3372 【【模板】线段树 1】(珂朵莉树解法)

    这道题可以用珂朵莉树做,但是由于数据比较不随机,而我也没有手写一颗平衡树,所以就被卡掉了,只拿了70分. 珂朵莉树是一种基于平衡树的(伪)高效数据结构. 它的核心操作是推平一段区间. 简而言之,就是把 ...