gpt4 book ai didi

android - 在另一个布局中扩充一个布局

转载 作者:行者123 更新时间:2023-11-29 01:03:05 26 4
gpt4 key购买 nike

我正在研究抽屉导航布局。必须加载另一个布局来代替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);

}

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

最佳答案

你必须创建 fragment (右键单击您的项目 -> New -> Fragment).然后在下面的 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();

}

关于android - 在另一个布局中扩充一个布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49605807/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com