Mega Code Archive

 
Categories / Android / UI
 

Using two layout xml file for one Activity

package app.test; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.widget.LinearLayout; import android.widget.RelativeLayout; public class Test extends Activity {   /** Called when the activity is first created. */   @Override   public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     LinearLayout layoutMain = new LinearLayout(this);     layoutMain.setOrientation(LinearLayout.HORIZONTAL);     setContentView(layoutMain);     LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);     RelativeLayout layoutLeft = (RelativeLayout) inflate.inflate(         R.layout.main, null);     RelativeLayout layoutRight = (RelativeLayout) inflate.inflate(         R.layout.row, null);     RelativeLayout.LayoutParams relParam = new RelativeLayout.LayoutParams(         RelativeLayout.LayoutParams.WRAP_CONTENT,         RelativeLayout.LayoutParams.WRAP_CONTENT);     layoutMain.addView(layoutLeft, 100, 100);     layoutMain.addView(layoutRight, relParam);   } } //main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/left"   xmlns:android="http://schemas.android.com/apk/res/android"   android:layout_width="fill_parent"   android:layout_height="fill_parent">   <TextView android:id="@+id/view1" android:background="@drawable/icon"     android:layout_width="fill_parent"     android:layout_height="50px" android:text="1" />   <TextView android:id="@+id/view2"     android:background="@drawable/icon"     android:layout_width="fill_parent"     android:layout_height="50px" android:layout_below="@id/view1"     android:text="2" /> </RelativeLayout> //row.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/right"   xmlns:android="http://schemas.android.com/apk/res/android"   android:layout_width="fill_parent"   android:layout_height="fill_parent">   <TextView android:id="@+id/right_view1"     android:background="@drawable/icon" android:layout_width="fill_parent"     android:layout_height="wrap_content" android:text="3" />   <TextView android:id="@+id/right_view2"     android:background="@drawable/icon"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:layout_below="@id/right_view1" android:text="4" /> </RelativeLayout>