在main.xml中包含aaa.xml,bbb.xml
aaa.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="a" /> </LinearLayout>
bbb.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="a" /> </LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <include android:id="@+id/a" layout="@layout/aaa" /> <include android:id="@+id/b" layout="@layout/bbb" /> </LinearLayout>
效果图
获取组件
public class LayoutTestActivity extends Activity { private LinearLayout l1, l2; private TextView tv1, tv2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); l1 = (LinearLayout) findViewById(R.id.a); tv1 = (TextView) l1.findViewById(R.id.tv); l2 = (LinearLayout) findViewById(R.id.b); tv2 = (TextView) l2.findViewById(R.id.tv); tv1.setText("aaaaaaaaaaaaaaaaaaaaaaaa"); tv2.setText("bbbbbbbbbbbbbbbbbbbbbbbb"); } }
效果图