Android的 - 编写自定义(化合物)成分 [英] Android - Writing a custom (compound) component
问题描述
Android的应用程序,我目前正在开发具有增长相当大的一个主要活动。这主要是因为它包含了一个 TabWidget
有3个标签。每个标签都有相当多的部件。活动具有控制所有这些部件中的一次。所以我觉得你可以想像,这一活动已经像20场(几乎每一个组件的字段)。此外,它含有大量的逻辑(点击监听器,逻辑填写列表等)。
The Android app I'm currently developing has a main activity that has grown quite large. This is mainly because it contains a TabWidget
with 3 tabs. Each tab has quite a few components. The activity has to control of all those components at once. So I think you can imagine that this Activity has like 20 fields (a field for almost every component). Also it contains a lot of logic (click listeners, logic to fill lists, etc).
我通常做的基于组件框架是分割一切成自定义组件。每个自定义组件然后将有明确的责任。它会包含它自己的一套组件和所有其他相关的逻辑该组件。
What I normally do in component based frameworks is to split everything up into custom components. Each custom component would then have a clear responsibility. It would contain it's own set of components and all other logic related to that component.
我试图找出如何可以做到这一点,我发现了一些在Android文档什么,他们喜欢称之为复合控制研究。 (请参阅<一个href="http://developer.android.com/guide/topics/ui/custom-components.html#compound">http://developer.android.com/guide/topics/ui/custom-components.html#compound并滚动到复合控件一节),我想基于XML文件中定义的视图结构来创建这样一个组成部分。
I tried to figure out how this can be done, and I found something in the Android documentation what they like to call a "Compound Control". (See http://developer.android.com/guide/topics/ui/custom-components.html#compound and scroll to the "Compound Controls" section) I would like to create such a component based on an XML file defining the view structure.
在文档,它说:
注意,就像处理活动, 您可以使用声明 (基于XML的)的方式来创建 包含的组件,或者你可以嵌套 他们编程方式从您的code。
Note that just like with an Activity, you can use either the declarative (XML-based) approach to creating the contained components, or you can nest them programmatically from your code.
嗯,这是个好消息!基于XML的方法正是我想要的!但它没有说怎么办呢,除了它是像一个活动......但我做的一个活动就是调用的setContentView(...)
膨胀从XML的观点。这个方法不可用,如果你如子类的LinearLayout
。
Well, that's good news! The XML-based approach is exactly what I want! But it doesn't say how to do it, except that it is "like with an Activity"... But what I do in an Activity is call setContentView(...)
to inflate the views from XML. That method is not available if you for example subclass LinearLayout
.
于是,我就手动充气XML是这样的:
So I tried to inflate the XML manually like this:
public class MyCompoundComponent extends LinearLayout {
public MyCompoundComponent(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.my_layout, this);
}
}
这只是我是在加载XML有事实的LinearLayout
声明为根元素的作品。这导致虚增的LinearLayout
是 MyCompoundComponent
的孩子这本身就已经是一个的LinearLayout
!所以,现在我们之间有 MyCompoundComponent
冗余的LinearLayout中,它实际需要。
This works, except for the fact that the XML I'm loading has LinearLayout
declared as the root element. This results in the inflated LinearLayout
being a child of MyCompoundComponent
which itself already is a LinearLayout
!! So now we have a redundant LinearLayout in between MyCompoundComponent
and the views it actually needs.
有人可以请给我提供一个更好的方式来处理这一点,避免有多余的的LinearLayout
实例?
Can somebody please provide me with a better way to approach this, avoiding having a redundant LinearLayout
instantiated?
推荐答案
使用合并标记作为XML根
Use merge tag as your XML root
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Your Layout -->
</merge>
这篇关于Android的 - 编写自定义(化合物)成分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!