充气布局自定义控件 - 如何? [英] Inflating layout in custom control - how?
问题描述
我有和想法对此处创建自定义的控制: <一href="http://stackoverflow.com/questions/5891153/android-interface-need-suggestions-on-what-widgets-to-use">Android接口 - 什么部件使用需要建议
I've got and idea on how to create custom control here: Android interface - need suggestions on what widgets to use
我如何做相同的,但在创建XML控件的布局,只是夸大它在code?不喜欢在这个例子中,我必须手动创建每个控件。
How do I make the SAME but creating control's layout in XML and just inflating it in code? Not like in this example where I have to create each control manually.
我的第一个问题的LinearLayout是用作基础不支持的setView()命令。如果我向其他什么东西?
My first problem that LinearLayout that used as a base does not support setView() command. Should I extend something else?
编辑:我发现 本: <一href="http://developer.android.com/guide/topics/ui/custom-components.html">http://developer.android.com/guide/topics/ui/custom-components.html 和这个: <一href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List4.html">http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List4.html
I found This: http://developer.android.com/guide/topics/ui/custom-components.html and this: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List4.html
好像我需要的复合控制。我只需要code点点。我如何从充气XML控件的内容?无论文章和样品说我可以,但怎么样?
Seems like I need COMPOUND CONTROL. I just need little bit of code. How do I inflate control's content from XML? BOth article and sample say I can but HOW?
推荐答案
您必须使用的布局是这样的:
You have to use a layout like this:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<View android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="#0F0"/>
<View android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="#0FF"/>
</merge>
其中,&LT;合并&GT;
的意思是把一切的我内心到父我将充气至
where <merge>
means "put everything that's inside me into the parent I'm going to be inflated to".
然后在code:
public class CControl extends LinearLayout {
public CControl(Context context) {
this(context, null);
}
public CControl(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.tryout, this);
}
// ......
}
在这一点上,你可以用你的复合控制,就好像它是一个LinearLayout中,所以你必须要在外部布局指定,如果你希望它是垂直的,例如,或者你可以将其设置为内部的默认构造函数。
At this point you can use your compound control as if it's a LinearLayout, so you'll have to specify in the outer layout if you want it to be vertical, for example, or you could set it as a default inside the constructor.
这篇关于充气布局自定义控件 - 如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!