【问题标题】:Andriod Navigation Component 2.1.0 with java带有java的Android导航组件2.1.0
【发布时间】:2019-11-20 23:32:02
【问题描述】:

当我尝试在 java 上使用 android 导航组件时。它不适用于开箱即用的导航抽屉。除了从导航抽屉导航到片段之外,我能够执行所有操作。 我正在做一个项目,我需要快速摆脱困境,并想用我更有经验的 java 来完成它。有没有人能够破解这个?

implementation 'androidx.navigation:navigation-fragment:2.1.0'
implementation 'androidx.navigation:navigation-ui:2.1.0'

MainActivity onCreate

rootLayout = findViewById(R.id.drawer_layout);

    drawerLayout = findViewById(R.id.drawer_layout);

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    NavHostFragment navHostFragment = (NavHostFragment)getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
    navController = navHostFragment.getNavController();

    mAppBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph())
            .setDrawerLayout(drawerLayout)
            .build();

    drawerLayout = findViewById(R.id.drawer_layout);

    //setup navigation
    NavigationView navigationView = findViewById(R.id.nav_view);
    NavigationUI.setupWithNavController(navigationView, navController);
    mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_claims, R.id.nav_products,R.id.nav_deliveries, R.id.nav_quotes,
            R.id.nav_policies, R.id.nav_payment,R.id.nav_about,R.id.nav_contact)
            .setDrawerLayout(drawerLayout)
            .build();

    //setup action bar
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);

PS:mobile_navigation.xml和activity_main_drawer.xml上的id是一样的。所以没有问题。

【问题讨论】:

    标签: java android kotlin android-architecture-navigation


    【解决方案1】:

    发现问题。这有点奇怪,但是哦……您需要确保 app_bar_main 的包含在您的活动中的 NavigationView 之前,例如 activity_main

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer"
        android:background="@color/colorAccent"
        app:itemTextColor="@android:color/white"
        app:itemIconTint="@android:color/white"
        android:fitsSystemWindows="false"
        android:isScrollContainer="true"
        android:saveEnabled="true"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多