//新建一个碎片
public class LeftFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// 加载一个碎片界面
View view = inflater.inflate(R.layout.leftfragment, container, false);
return view;
}
}
//动态加载
//1,得到碎片管理器
FragmentManager fragmentManager=getFragmentManager();
//2,开启事务
FragmentTransaction transaction=fragmentManager.beginTransaction();
//要做的事件,加载碎片
LeftFragment leftFragment=new LeftFragment();
transaction.add(R.id.fl, leftFragment, "left_fragment");//加载左边的碎片,取名为f1,第一个参数是说此碎片放在此布局的哪个位置
//3,提交事务
transaction.commit();
//或者一句写完
getFragmentManager().beginTransaction().add(R.id.fl,new LeftFragment(), "left_fragment").commit();
<!-- 静态通过代码加载碎片 -->
<fragment android:name="comruicaiedu.day0106_fragment.LeftFragment"
android:id="@+id/left"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"/>
<fragment android:name="comruicaiedu.day0106_fragment.RightFragment"
android:id="@+id/right"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>