创建自定义视图 [英] Creating custom view
问题描述
我想创建一个自定义视图 TestView
类中,我可以通过新TestView创建对象()
。
然而,一个新视图类需要的AttributeSet对象。从我在哪里拿到的AttributeSet?它必须包括?
I want to create a custom view TestView
class for which I can create object via new TestView()
.
A new view class however needs a AttributeSet object. From where do I get that AttributeSet and what does it have to include?
推荐答案
这不是强制性的,大多数时候,你甚至不必担心它,只要你提供了从查看<构造函数/ code>是通过他们对
超()
。
It's not mandatory, and most times you don't even have to worry about it as long as you provide constructors from View
that pass them along to super()
.
public CustomView(Context context) // No Attributes in this one.
{
super(context);
// Your code here
}
public CustomView(Context context, AttributeSet attrs)
{
super(context, attrs);
// Your code here
}
public CustomView(Context context, AttributeSet attrs, int default_style)
{
super(context, attrs, default_style);
// Your code here
}
查看
需要做繁重的处理所有的 Android系统的保养要点:*
属性,你'添加视图布局当D通常是通过研究。你的构造能够利用这些属性,或者你自己,如果你定义了他们:
View
takes care of doing the heavy lifting for dealing with all of the android:*
attributes that you'd usually pass in when adding the view to a layout. Your constructors could make use of those attributes or your own if you've defined them:
<com.blrfl.CustomView
android:id="@+id/customid"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
blrfl:foo="bar"
blrfl:quux="bletch"
/>
这篇关于创建自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!