在MainActivity布局中加入 frameLayout 控件
以下是对控件的操作
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
//隐藏所以Fragment
hideFragment(transaction)
//插入 MyFragment()自定义的
Fragment mTab01 = new MyFragment();
//R.id.id_content是frameLayout 控件id
transaction.add(R.id.id_content, mTab01);
transaction.show(mTab01);
//commit()后才能生效
transaction.commit()后才能生效
ps:安卓3.0一下fragment统一用。Activity也要extendsFragmentActivity
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
http://www.imooc.com/video/5903