public class MyFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_layout, container, false); // 绑定视图到控件 return view; } }
MyFragment myFragment = new MyFragment(); getSupportFragmentManager().beginTransaction() .add(R.id.fragment_container, myFragment).commit();
如果使用了ViewPager等带有Fragment的库,还需要注意FragmentPagerAdapter的使用方式是否正确。