在搜索之后,我无法全面迈向AndroidX。我的导航抽屉有问题。

<android.support.design.internal.BottomNavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main_page"
    app:menu="@menu/activity_main_page_drawer" />


我已将navigationView更改为bottomNavigationView。

    //NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    BottomNavigationView bottomNavView = (BottomNavigationView) findViewById(R.id.nav_view);
    bottomNavView.setNavigationItemSelectedListener(this);
    View headerView = bottomNavView.getHeaderView(0);
    TextView navUsername = (TextView) headerView.findViewById(R.id.textnav);
    navUsername.setText(str);


public class MainPageActivity extends AppCompatActivity
    implements BottomNavigationView.OnNavigationItemSelectedListener {


但是我不起作用。现在出现其他错误,

错误:


  找不到符号方法
  setNavigationItemSelectedListener(MainPageActivity)
                bottomNavView.setNavigationItemSelectedListener(this);
                               ^符号:方法setNavigationItemSelectedListener(MainPageActivity)
       找不到符号方法getHeaderView(int)View headerView = bottomNavView.getHeaderView(0);
                                             ^符号:方法getHeaderView(int)


我该如何解决所有这些错误?

最佳答案

您应该对headerlayout使用navigation drawer而不是BottomNavigation

AndroidX是对原始Android Support Library.的重大改进
因此要使项目使用androidX。


转到Refactor-> Migrate to androidX.
implementation com.google.android.material.material:1.1.0-alpha09处添加buid.gradle


在您的xml中:

<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <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"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_home"
        app:menu="@menu/menu_home_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>


在您的Java文件中:

 navigationView.setNavigationItemSelectedListener(YourClass.this);


内部应用程序级别主题:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

09-10 01:59
查看更多