简体   繁体   English

从抽屉活动示例更改布局

[英]Change layout from the drawer activity example

I've created a new project, starting with the "Navigation drawer activity" set by default. 我创建了一个新项目,从默认设置的“导航抽屉活动”开始。 I wanted to change the layout when a button of the navigation drawer is pressed. 我想在按下导航抽屉的按钮时更改布局。

In the MainActivity.java, I added the last line here: 在MainActivity.java中,我在这里添加了最后一行:

@SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {
            setContentView(R.layout.app_bar_main2);
        ...

I've also created a new layout named app_bar_main2, and added a basic TextView. 我还创建了一个名为app_bar_main2的新布局,并添加了一个基本的TextView。

I've not done anything else (which might be why it's not working), and when I run the app and click on the gallery icon, the app closes itself. 我没有做任何其他事情(这可能是为什么它不起作用的原因),当我运行该应用程序并单击图库图标时,该应用程序将自行关闭。

What have I to do to make it work? 我该怎么做才能使其正常工作?

When you click on any option in the Navigation Drawer Activity in Android, it opens a fragment on the main UI and closes the earlier fragment by itself. 当您在Android的“ Navigation Drawer Activity中单击任何选项时,它将在主UI上打开一个片段,并自行关闭之前的片段。

All you need to do is make a Fragment visible on the UI on the selection of an option 您需要做的就是在选择一个选项时在用户界面上显示一个片段

Here's some help about Fragments 这是有关片段一些帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM