Fragment demo

jackie_gnu發表於2011-08-11

 left fragment:

package com.easymorse.demos;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class DemoLeftFragment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.demo_left_fragment, container,false);
	}
}

demo_left_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" android:layout_height="wrap_content"
	android:text="demo left fragment" />


 


Right fragment:

package com.easymorse.demos;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class DemoRightFramgment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.demo_right_fragment, container,false);
	}
}

demo_right_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" android:layout_height="wrap_content"
	android:text="demo right fragment" />


 


Activity:

package com.easymorse.demos;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

public class FragmentDemoActivity extends FragmentActivity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
    //START.for first main.xml
		FragmentManager fragmentManager=getSupportFragmentManager();
		FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
		DemoLeftFragment leftFragment=new DemoLeftFragment();
		DemoRightFramgment rightFramgment=new DemoRightFramgment();
		fragmentTransaction.add(R.id.leftView, leftFragment);
		fragmentTransaction.add(R.id.rightView, rightFramgment);
		fragmentTransaction.commit();
    // END. }

}


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="horizontal" android:layout_width="fill_parent"
	android:layout_height="fill_parent" android:id="@+id/rootView">
	<LinearLayout android:id="@+id/leftView"
		android:orientation="vertical" android:layout_width="0dp"
		android:layout_weight="1" android:layout_height="fill_parent" 
		android:background="#c0c0c0c0"/>
	<LinearLayout android:id="@+id/rightView"
		android:orientation="vertical" android:layout_width="0dp"
		android:layout_weight="2" android:layout_height="fill_parent" />
</LinearLayout>

or main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="horizontal" android:layout_width="fill_parent"
 android:layout_height="fill_parent" android:id="@+id/rootView">
 
 <fragment class= "com.easymorse.demos.DemoLeftFragment" android:id="@+id/leftView"
  android:orientation="vertical" android:layout_width="0dp"
  android:layout_weight="1" android:layout_height="fill_parent" 
  android:background="#c0c0c0c0"/>
  
 <fragment class="com.easymorse.demos.DemoRightFragment" android:id="@+id/rightView"
  android:orientation="vertical" android:layout_width="0dp"
  android:layout_height="fill_parent" android:layout_weight="1"/>
</LinearLayout>



 

相關文章