我正在尝试使用新的 Android 架构设置导航抽屉布局和应用栏配置。我遇到的问题是 android studio 告诉我设置抽屉布局的方式已被弃用。
这是我的xml配置
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/top_app_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/MaterialTheme"
style="@style/Widget.MaterialComponents.Toolbar.Primary"
/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.drawerlayout.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation_graph"/>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="230dp"
android:layout_height="match_parent"
android:background="@drawable/gradient_background"
app:itemTextColor="@android:color/black"
app:menu="@menu/drawer_menu"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:theme="@style/Theme.Design">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
这是我声明 AppBarConfiguration 和 Drawer Layout 的方式
private AppBarConfiguration appBarConfiguration;
private NavController navController;
MaterialToolbar topAppBar;
private ActionBarDrawerToggle actionBarDrawerToggle;
DrawerLayout drawerLayout;
NavigationView navigationView;
这里是我找到 NavController、topAppBar、drawerLayout 和 NavigationView 的地方
navController = Navigation.findNavController(this, R.id.nav_host_fragment);
topAppBar = findViewById(R.id.top_app_bar);
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.navigation_view);
这是设置已弃用的抽屉布局的方法是否有一种设置未弃用的抽屉布局的新方法
appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph())
.setDrawerLayout(drawerLayout)
.build();
这是设置 NavigatioUi 的地方
NavigationUI.setupWithNavController(topAppBar, navController, appBarConfiguration);
setOpenableLayout(@Nullable Openable openableLayout)
方法
AppBarConfiguration.Builder
DrawerLayout
实现 Openable
。
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph())
.setOpenableLayout(drawerLayout)
.build();
具有讽刺意味的是,最新的 Android Studio(v 4.0.1)本身在其内置的
setDrawerLayout(drawer)
示例中使用了 NavigationDrawerActivity
。
修复方法非常简单。这可能就是你所拥有的:
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setDrawerLayout(drawer) // REPLACE THIS LINE
.build();
将上述行替换为以下内容:
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setOpenableLayout(drawer) // WITH THIS LINE
.build();
来源:Android官方参考
DrawerLayout
类实现了Openable
。因此,这将简单地工作,而不需要您进行任何其他更改。