运行时创建的复合Android组件

运行时创建的复合Android组件,android,custom-view,composite-component,Android,Custom View,Composite Component,问候。我正在做一个项目,我想我会卷起一个复合组件。我用xml对它们进行了布局,并编写了一个扩展LinearLayout的新类。基本上,我在一项活动中需要大约五六个。我想以编程方式在活动内部创建这些,因为我不知道在运行前需要多少。此外,我还创建了一个xml文件来设置它们,我在下面没有包括这个文件。我发现每种方法都有问题 下面是我创建的类中扩展LinearLayout的方法段: public ExtendedLinearLayout(Context context, AttributeSet att

问候。我正在做一个项目,我想我会卷起一个复合组件。我用xml对它们进行了布局,并编写了一个扩展LinearLayout的新类。基本上,我在一项活动中需要大约五六个。我想以编程方式在活动内部创建这些,因为我不知道在运行前需要多少。此外,我还创建了一个xml文件来设置它们,我在下面没有包括这个文件。我发现每种方法都有问题

下面是我创建的类中扩展LinearLayout的方法段:

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

@Override
protected void onFinishInflate() {
    super.onFinishInflate();
    ((Activity)getContext()).getLayoutInflater().inflate(R.layout.extended_linear_layout, this);
    ...
}
在活动中,这两个解决方案似乎与我所寻找的最接近

不知道使用此方法从何处获取属性:

ExtendedLinearLayout customViewClass = new ExtendedLinearLayout(this, attrs);
layout.addView(customViewClass)
LayoutInflater inflater = LayoutInflater.from(this);
inflater.inflate(R.layout.extended_linear_layout, parent_layout);
不知道如何指定充气机应使用此方法的ExtendedLinearLayout:

ExtendedLinearLayout customViewClass = new ExtendedLinearLayout(this, attrs);
layout.addView(customViewClass)
LayoutInflater inflater = LayoutInflater.from(this);
inflater.inflate(R.layout.extended_linear_layout, parent_layout);
我可能会满足于使用ListView,但对于五到六个项目来说,这似乎有点过头了。而且,在使用Flex之前,这至少看起来是一个合理的问题解决方案

谢谢, 兰德尔

----------------更新-------------------

我已经尝试了下面讨论的方法。一旦我设置好所有内容,就会得到一个膨胀异常:二进制XML文件行#8:错误膨胀类。我觉得xml看起来很好。有一件事我可能会感到困惑,那就是在哪里给充气机打电话。现在,我在自定义类的onFinishInflate()中以及在活动中调用充气器,如下所示:

public class ExtendedLinearLayout extends LinearLayout {
    ...
public ExtendedLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.extended_linear_layout, null);
        ...
    }
}
在活动中:

public class TestActivity extends Activity {
    LinearLayout list;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        list = (LinearLayout) findViewById(R.id.simple_list);
        runTestCode();
    }
    private void runTestCode() {
        LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ExtendedLinearLayout layoutOne = 
                (ExtendedLinearLayout) inflater.inflate(R.layout.extended_linear_layout, list);
    }
}
谢谢,希望你有时间看一看。我喜欢在网上翻来翻去,但从昨天早上开始,这件事已经让我煮了好几个小时了。我离把它重构成ListAdapter还有2英寸的距离

----------------更新-------------------

这是xml文件

<?xml version="1.0" encoding="utf-8"?>
<com.anotherandroidblog.ExtendedLinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    >
    <TextView
        android:id="@+id/text_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        ></TextView>
    <TextView
        android:id="@+id/text_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        ></TextView>
    <TextView
        android:id="@+id/text_three"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        ></TextView>
</com.anotherandroidblog.ExtendedLinearLayout>

您需要在XML文件中使用完整的类名:而不是使根元素
LinearLayout
成为
ExtendedLinearLayout
的完整类名(包括包名)。然后
LayoutInflater
将知道该做什么


编辑:另外,
attrs
选项实际上并不可行(或者至少我不这么认为)。我还没有找到一种方法来在运行时生成实际的
attrs
对象实例:没有公共构造函数,我想这是框架在后台做的另一件不可访问的事情。

太棒了,感谢忍者风格的超快速响应。我会把它放进去,过几天再告诉你。另外,是的-从我所读到的(包括Romain Guy在类似帖子中的回复),你应该总是使用充气器方法而不是addView()方法。我现在已经运行了好几次移动东西和挖掘以使其正确。我发现了一个充气异常,我认为它与我何时以及如何给充气机打电话有关。我在上面的原始帖子中添加了一个更新。还添加了布局XML,以便我可以看到它的样子。您缺少
R.layout.lineup\u list\u项
:这似乎就是发生充气错误的地方,不是吗?再加上这个。对不起,那是个转置错误。除了活动的main.xml之外,只有一个xml文件。上面的xml文件名为extended_linear_layout.xml