简体   繁体   English

AndroidX导航抽屉

[英]AndroidX Navigation Drawer

After searching and searching I can't make the full step to AndroidX. 在搜索之后,我无法全面迈向AndroidX。 I have problems with Navigation Drawer. 我的导航抽屉有问题。

<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" />

I have changed navigationView to bottomNavigationView. 我已将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 {

But I don't get it to work. 但是我不起作用。 Now other errors appear, 现在出现其他错误,

Error: 错误:

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

How can I solve all these errors? 我该如何解决所有这些错误?

You should use headerlayout for navigation drawer instead of BottomNavigation 您应该将headerlayout用于navigation drawer而不是BottomNavigation

AndroidX is a major improvement to the original Android Support Library. AndroidX是对原始Android Support Library.的重大改进Android Support Library. So to make project use androidX. 因此要使项目使用androidX。

  • Goto Refactor-> Migrate to androidX. 转到Refactor-> Migrate to androidX.

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

In your xml: 在您的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>

In your java file: 在您的Java文件中:

 navigationView.setNavigationItemSelectedListener(YourClass.this);

Inside app level theme: 内部应用程序级别主题:

<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>

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

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