最簡單的,你做到這一點與包括在主佈局
<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類中包含函數。 –