Friday, 5 April 2013

Using two layout xml file for one Activity

Using two layout xml file for one Activity


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 = (LayoutInflatergetSystemService(Context.LAYOUT_INFLATER_SERVICE);
    RelativeLayout layoutLeft = (RelativeLayoutinflate.inflate(
        R.layout.main, null);
    RelativeLayout layoutRight = (RelativeLayoutinflate.inflate(
        R.layout.row, null);

    RelativeLayout.LayoutParams relParam = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
    layoutMain.addView(layoutLeft, 100100);
    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>

No comments:

Post a Comment