主要步骤
1、写出fragment的布局文件
2、定义一个继承Fragment的类加载该布局文件
3、fragment的调用
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction(); fragmentTransaction.replace(android.R.id.content,new Fragment1());
fragmentTransaction.commit();
定义一个继承Fragment的类
public class Fragment1 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//加载布局文件
View view = inflater.inflate(R.layout.fragment_1, null);
return view;
}
}
主函数中根据横竖屏加载不同fragment
//获取屏幕分辨率
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
Point point = new Point();
wm.getDefaultDisplay().getSize(point);
int x= point.x;
int y = point.y;
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();//开启事务
if(y>x){
//横屏
fragmentTransaction.replace(android.R.id.content,new Fragment1());
}else{
//竖屏
fragmentTransaction.replace(android.R.id.content,new Fragment1());
}
//☆☆☆☆☆ 提交事务
fragmentTransaction.commit();
```