【问题标题】:Inflate a layout in another layout在另一个布局中膨胀一个布局
【发布时间】:2018-09-11 08:20:24
【问题描述】:

我正在研究导航抽屉布局。必须加载另一个布局来代替 导航项的drawer_layout onClick。

 if (id == R.id.nav_camera) {

  //code to inflate another layout inplace of current layout
  //setContentView(R.layout.another_layout1);


    } else if (id == R.id.nav_gallery) {

  //code to inflate another layout inplace of current layout
  //setContentView(R.layout.another_layout2);

    } 

如何在主布局中加载另一个布局?

【问题讨论】:

标签: android android-layout navigation-drawer


【解决方案1】:

你必须创建片段 (右键单击您的项目 -> 新建 -> 片段)。然后在此下方的 Activity 中加载..

 if (id == R.id.nav_camera) {

     CameraFragment camera = new CameraFragment();
     FragmentManager fragmentManager = getFragmentManager();
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();              
     fragmentTransaction.add(R.id.fragment_container, camera, "HELLO");
     fragmentTransaction.commit();


    } else if (id == R.id.nav_gallery) {

     GalleryFragment gallery = new GalleryFragment();
     FragmentManager fragmentManager = getFragmentManager();
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();              
     fragmentTransaction.add(R.id.fragment_container, gallery, "HELLO");
     fragmentTransaction.commit();

    } 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 2011-10-05
    • 1970-01-01
    相关资源
    最近更新 更多