0

我创建了一个如下表单(form.xml):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/formLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android :text="Name" />

    <EditText
        android:id="@+id/edName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

<!-- Many more things-->
</RelativeLayout>

如果上面的表单是通过代码创建的(比如 FormView),那么在每个活动中我都可以使用类似的东西

<com.example.FormView
    android:id="@+id/fv1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

但是由于表单是使用 xml 创建的,如上所示,我如何在众多活动中使用它?
我想在所有活动中使用上面的表单布局(form.xml)作为用户控件。


我尝试了以下操作,但它NullPointerException在最后一行抛出:

setContentView(R.layout.main);
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.container);
View form= (View) findViewById(R.id.formLayout);
mainLayout.addView(form);

任何建议表示赞赏。

4

2 回答 2

1

看看这里重用布局

它展示了如何重用现有布局以将其包含在其他布局中。

于 2012-11-03T11:13:05.007 回答
-1

你到底想做什么,然后我会为你编写代码,使用下面的代码并将你的小部件放入其中并完成......

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.XMLFileName);
于 2012-11-02T07:50:02.933 回答