鸟语天空
动态添加碎片
post by:追风剑情 2015-10-12 20:44

xml视图参见

碎片——Fragment


package com.example.androidtest;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Display;
import android.view.WindowManager;

public class FragmentsActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fragments);
		
		FragmentManager fragmentManager = getFragmentManager();
		FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
		
		WindowManager wm = getWindowManager();
		Display d = wm.getDefaultDisplay();
		if(d.getWidth() > d.getHeight()){//landscape mode
			Fragment1 fragment1 = new Fragment1();
			//android.R.id.content活动的内容视图
			fragmentTransaction.replace(android.R.id.content, fragment1);
		}else{//portrait mode
			Fragment2 fragment2 = new Fragment2();
			fragmentTransaction.replace(android.R.id.content, fragment2);
		}
		fragmentTransaction.commit();
	}
}


评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容