动态添加碎片

作者:追风剑情 发布于:2015-10-12 20:44 分类:Android

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();
	}
}


标签: Android

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号