AndroidX导航抽屉

3

我一直在搜索,但是我还不能完全迁移到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)

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


1
请提供错误堆栈跟踪。 - Rajnish suryavanshi
1个回答

6

您应该使用headerlayout来替代BottomNavigation作为导航抽屉

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

  • 转到重构->迁移到AndroidX

  • buid.gradle中添加implementation com.google.android.material.material:1.1.0-alpha09

在您的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>

找不到符号方法setNavigationItemSelectedListener(MainPageActivity) - diaconu liviu
很高兴能帮到你! - Sumit Shukla

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接