我正在尝试使用新的Android架构设置带有App Bar配置的导航抽屉布局。我的问题是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和抽屉布局的方法
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);发布于 2020-06-26 17:31:33
使用setOpenableLayout(@Nullable Openable openableLayout)你的drawerLayout是可打开的。
发布于 2020-09-10 19:40:27
具有讽刺意味的是,最新的Android Studio (v4.0.1)本身在其内置的NavigationDrawerActivity示例中使用了setDrawerLayout(drawer)。
修复方法非常简单。这可能是你所拥有的:
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。因此,这将简单地工作,而不需要从您的端进行任何其他更改。
https://stackoverflow.com/questions/62386279
复制相似问题