Android studio setDrawerLayout(androidx.drawerlayout.widget.drawerlayout)不推荐使用
我正在尝试使用新的Android架构设置带有应用程序栏配置的导航抽屉布局。我遇到的问题是,android studio告诉我,我设置抽屉布局的方式不受欢迎 这是我的xml配置Android studio setDrawerLayout(androidx.drawerlayout.widget.drawerlayout)不推荐使用,android-studio,uinavigationcontroller,navigation-drawer,appbar,Android Studio,Uinavigationcontroller,Navigation Drawer,Appbar,我正在尝试使用新的Android架构设置带有应用程序栏配置的导航抽屉布局。我遇到的问题是,android studio告诉我,我设置抽屉布局的方式不受欢迎 这是我的xml配置 <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/to
<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>
在这里我可以找到NavController、topAppBar、抽屉布局和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();
这里是我设置导航UI的地方
NavigationUI.setupWithNavController(topAppBar, navController, appBarConfiguration);
使用
setOpenableLayout(@Nullable openableLayout)
您的抽屉布局是可打开的。讽刺的是,最新的Android Studio(V4.0.1)本身在其内置的
NavigationDrawerActivity
示例中使用了setDrawerLayout(抽屉)
解决方法非常简单。以下可能是您所拥有的:
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();
资料来源:
DrawerLayout
类实现了Openable
。因此,这将在不需要任何其他更改的情况下工作。我们如何使用此方法,您是否也可以添加此代码,我认为这将有助于其他人。appBarConfiguration=new appBarConfiguration.Builder(navController.getGraph()).setOpenableLayout(drawerLayout).build();在哪里?在Drawer主类中?请参阅包androidx.drawerlayout.widget。公共类DrumerLayout扩展了ViewGroup,实现了可打开的
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setOpenableLayout(drawer) // WITH THIS LINE
.build();