Android 如何以编程方式添加到布局复合组件?

Android 如何以编程方式添加到布局复合组件?,android,android-layout,view,custom-component,Android,Android Layout,View,Custom Component,我创建了一个复合组件框,希望将其添加到布局中框xml: <?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayoutForBlock" android:layout_width="wrap_content" android:layout_height

我创建了一个复合组件
,希望将其添加到布局中<代码>框xml:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayoutForBlock"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" android:background="@drawable/screen_background" android:layout_marginLeft="5dp" android:layout_marginTop="5dp">
        <ImageButton
            android:id="@+id/imageButtonContent"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center_horizontal"
            android:scaleType="fitCenter"
            android:src="@drawable/beach_bed" android:background="@drawable/buttonbackground" android:clickable="true" android:layout_margin="5dp" android:contentDescription="@string/sample_text"/>
        <TextView
            android:id="@+id/textViewContent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/sample_text"
            android:textColor="@color/deep_blue" android:layout_margin="5dp"/>
    </LinearLayout>
</merge>
如果我将
框添加到主xml文件中,则一切正常,如:

<com.mypackage.alexey.Box
android:id="@+id/mybox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
/> 


问题是我想通过编程方式创建许多框,比如:
Box mybox=newbox()。如何实现?

我建议您也实现只需要
上下文的
线性布局的构造函数:

public Box(Context context) {
    super(context);
}
然后在
活动中
中,当您想要添加新的
时,实例化
类并将其添加到布局中:

// I assumed you have a LinearLayout to which you want to add your Box
LinearLayout parent = (LinearLayout) findViewById(R.id.parent_id);
//create the Box and added to the parent above
Box theBox = new Box(this); // if you are in an Activity
//some LayoutParams to replicate your xml attributes
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
// set the Orientation for the Box
theBox.setOrientation(LinearLayout.HORIZONTAL);
//add the Box
parent.addView(theBox);

谢谢您的回答,但是当我尝试赋值时出现了一个问题:theBox.setTextContent(“新内容”);我得到异常(NullPointerException)@alexey,因为您实例化了
Box
类,并且
Box
是一个有效的对象,这意味着
NullPointerException
可能来自
textContent
null
(在布局中找不到)。我不知道如何调用
onFinishInflate
(但我怀疑它适用于xml布局),我建议您将代码移动到充气
R.layout.box
文件的位置,并在构造函数中设置视图(
setupViewItems()
),该构造函数采用简单的
上下文
,然后重试。也可以使用此版本的
充气
充气(R.layout.box,this,true)
// I assumed you have a LinearLayout to which you want to add your Box
LinearLayout parent = (LinearLayout) findViewById(R.id.parent_id);
//create the Box and added to the parent above
Box theBox = new Box(this); // if you are in an Activity
//some LayoutParams to replicate your xml attributes
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
// set the Orientation for the Box
theBox.setOrientation(LinearLayout.HORIZONTAL);
//add the Box
parent.addView(theBox);