Android 自定义视图构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2884501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Custom View Constructor
提问by Mitch
I'm learning about using Custom Views from the following:
我正在从以下方面学习使用自定义视图:
http://developer.android.com/guide/topics/ui/custom-components.html#modifying
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.
类初始化 一如既往,首先调用 super。此外,这不是默认构造函数,而是参数化构造函数。EditText 是在从 XML 布局文件膨胀时使用这些参数创建的,因此,我们的构造函数需要同时获取它们并将它们传递给超类构造函数。
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?
有没有更好的描述?我一直在试图弄清楚构造函数应该是什么样子,并且我提出了 4 种可能的选择(参见文章末尾的示例)。我不确定这 4 个选择做什么(或不做什么),为什么我应该实现它们,或者参数的含义。有这些描述吗?
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);
}
回答by CommonsWare
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.
第三个意味着您的自定义View
将可以从 XML 布局文件中使用。如果你不关心这个,你就不需要它。
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.
第四个是错误的,AFAIK。没有View
将 aMap
作为第三个参数的构造函数。有一个将 anint
作为第三个参数,用于覆盖小部件的默认样式。
I tend to use the this()
syntax to combine these:
我倾向于使用this()
语法来组合这些:
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.
您可以在本书示例中查看此代码的其余部分。
回答by yanchenko
Here's a my pattern (creating a custom ViewGoup
here, but still):
这是我的模式(ViewGoup
在这里创建自定义,但仍然):
// CustomView.java
public class CustomView extends LinearLayout {
public CustomView(Context context) {
super(context);
init(context);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context ctx) {
LayoutInflater.from(ctx).inflate(R.layout.view_custom, this, true);
// extra init
}
}
and
和
// view_custom.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Views -->
</merge>
回答by Jawad Zeb
When you are adding your custom View
from xml
like :
当您View
从xml
like添加自定义时:
<com.mypack.MyView
...
/>
you will need the public constructorMyView(Context context, AttributeSet attrs),
otherwise you will get an Exception
when Android
tries to inflate
your View
.
你将需要公共构造函数,MyView(Context context, AttributeSet attrs),
否则你会得到一个Exception
whenAndroid
尝试inflate
你的View
.
And when you add your View
from xml
and also specifythe android:style
attribute
like :
而当你添加View
的xml
,也说明了android:style
attribute
这样的:
<com.mypack.MyView
style="@styles/MyCustomStyle"
...
/>
you will also need the third public constructorMyView(Context context, AttributeSet attrs,int defStyle)
.
您还需要第三个公共构造函数MyView(Context context, AttributeSet attrs,int defStyle)
。
The third constructor is usually used when you extend a style and customize it, and then you would like to set that style
to a given View
in your layouts
第三个构造函数通常用于扩展样式并自定义它,然后您想将其设置style
为View
布局中的给定
Edit Details
编辑详情
public MyView(Context context, AttributeSet attrs) {
//Called by Android if <com.mypack.MyView/> is in layout xml file without style attribute.
//So we need to call MyView(Context context, AttributeSet attrs, int defStyle)
// with R.attr.customViewStyle. Thus R.attr.customViewStyle is default style for MyView.
this(context, attrs, R.attr.customViewStyle);
}