Android的自定义视图构造 [英] Android Custom View Constructor

查看:149
本文介绍了Android的自定义视图构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何使用自定义视图从以下方面:

<一个href="http://developer.android.com/guide/topics/ui/custom-components.html#modifying">http://developer.android.com/guide/topics/ui/custom-components.html#modifying

描述说:

  

类初始化与往常一样,   超被称为第一。此外,   这不是一个默认的构造函数,但   一个参数之一。该的EditText是   这些参数创建时   从一个XML布局文件充气,   因此,我们的构造需要两个   把它们并把它们传递给   超类的构造函数为好。

有没有更好的说明?我一直在试图找出构造(S)应该是什么样子,我已经提出了4种可能的选择(见例如在后结束)。我不知道这4个选择做(或不做),我为什么要实现这些,或者是参数的意思。是否有它们的描述?

 公共MyCustomView()
{
    超();
}

公共MyCustomView(上下文的背景下)
{
    超(上下文);
}

公共MyCustomView(上下文的背景下,ATTRS的AttributeSet)
{
    超(背景下,ATTRS);
}

公共MyCustomView(上下文的背景下,ATTRS的AttributeSet,地图PARAMS)
{
    超(背景下,ATTRS,则params);
}
 

解决方案

您不必第一个,因为这是行不通的。

第三个将意味着您的自定义查看将会从XML布局文件使用。如果你不在乎,你不需要它。

第四个是绝对错误的,AFAIK。没有查看构造采取地图作为第三个参数。有一个接受一个 INT 作为第三个参数,用于覆盖默认样式的小工​​具。

我倾向于使用这个()语法来组合这些:

 公共ColorMixer(上下文的背景下){
    这(背景下,NULL);
}

公共ColorMixer(上下文的背景下,ATTRS的AttributeSet){
    这(背景下,ATTRS,0);
}

公共ColorMixer(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
    超(背景下,ATTRS,defStyle);
    //这里真正的工作
}
 

您可以看到这个code在本书实例。

I'm learning about using Custom Views from the following:

http://developer.android.com/guide/topics/ui/custom-components.html#modifying

The description says:

Class Initialization As always, the super is called first. Furthermore, this is not a default constructor, but a parameterized one. The EditText is created with these parameters when it is inflated from an XML layout file, thus, our constructor needs to both take them and pass them to the superclass constructor as well.

Is there a better description? I've been trying to figure out what the constructor(s) should look like and I've come up with 4 possible choices (see example at end of post). I'm not sure what these 4 choices do (or don't do), why I should implement them, or what the parameters mean. Is there a description of these?

public MyCustomView()
{
    super();
}

public MyCustomView(Context context)
{
    super(context);
}

public MyCustomView(Context context, AttributeSet attrs)
{
    super(context, attrs);
} 

public MyCustomView(Context context, AttributeSet attrs, Map params)
{
    super(context, attrs, params);
} 

解决方案

You don't need the first one, as that just won't work.

The third one will mean your custom View will be usable from XML layout files. If you don't care about that, you don't need it.

The fourth one is just wrong, AFAIK. There is no View constructor that take a Map as the third parameter. There is one that takes an int as the third parameter, used to override the default style for the widget.

I tend to use the this() syntax to combine these:

public ColorMixer(Context context) {
    this(context, null);
}

public ColorMixer(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public ColorMixer(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // real work here
}

You can see the rest of this code in this book example.

这篇关于Android的自定义视图构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆