4

我尝试通过使用导航抽屉活动创建一个新项目。使用视图模型自动生成片段。单击导航菜单时,它不会导航到相应的片段。

构建.gradle

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.navigation:navigation-fragment:2.0.0'
    implementation 'androidx.navigation:navigation-ui:2.0.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

MainActivity.class

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
                R.id.nav_tools, R.id.nav_share, R.id.nav_send)
                .setDrawerLayout(drawer)
                .build();
        final NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }

@Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }

我用导航菜单验证了导航图中的 ID,它们是相同的。我是导航图的新手。任何帮助将非常感激。

4

1 回答 1

10

在您的activity_main布局中使用

<androidx.drawerlayout.widget.DrawerLayout>

  <include/>

  <com.google.android.material.navigation.NavigationView/>

</androidx.drawerlayout.widget.DrawerLayout>

代替

<androidx.drawerlayout.widget.DrawerLayout>

     <com.google.android.material.navigation.NavigationView/>

     <include/>

</androidx.drawerlayout.widget.DrawerLayout>

原因在文档中:

要使用 a DrawerLayout,请将您的主要内容视图定位为宽度和高度为且不为 > 的第一个子match_parent视图layout_gravity。在主要内容视图之后添加抽屉作为子视图并layout_gravity适当设置。抽屉通常match_parent用于具有固定宽度的高度。

于 2019-10-15T06:39:59.727 回答