最简单的,你做到这一点与包括在主布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="@layout/second" />
</LinearLayout>
It's也可以做到这一点编程的XML,但这种方式,我认为它是更清晰。
编辑: 要以编程方式执行此操作,请将此代码放入第一个按钮的侦听器中。
RelativeLayout view = (RelativeLayout) findViewById(R.id.RelativeLayout1);
Button b = new Button(getApplicationContext());
b.setText("Click me too!");
view.addView(b);
而不是创建一个按钮(或任何你想要的),你也可以膨胀预制布局。
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.second, null);
view.addView(v);
已编辑。简单地说,我想在Java类中包含函数。 –