Emm*_*ngo 13 appbar uinavigationcontroller android-studio navigation-drawer
我正在尝试使用新的 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>
Run Code Online (Sandbox Code Playgroud)
这是我如何声明 AppBarConfiguration 和 Drawer Layout
private AppBarConfiguration appBarConfiguration;
private NavController navController;
MaterialToolbar topAppBar;
private ActionBarDrawerToggle actionBarDrawerToggle;
DrawerLayout drawerLayout;
NavigationView navigationView;
Run Code Online (Sandbox Code Playgroud)
这是我找到 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);
Run Code Online (Sandbox Code Playgroud)
这是设置不推荐使用的抽屉布局的方式 是否有一种新的方式来设置不推荐使用的抽屉布局
appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph())
.setDrawerLayout(drawerLayout)
.build();
Run Code Online (Sandbox Code Playgroud)
这是我设置 NavigatioUi 的地方
NavigationUI.setupWithNavController(topAppBar, navController, appBarConfiguration);
Run Code Online (Sandbox Code Playgroud)
Adm*_*123 26
使用setOpenableLayout(@Nullable Openable openableLayout)
您的 drawerLayout 是实现 Openable。
Kat*_*hir 12
具有讽刺意味的是,最新的 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();
Run Code Online (Sandbox Code Playgroud)
将上述行替换为以下内容:
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setOpenableLayout(drawer) // WITH THIS LINE
.build();
Run Code Online (Sandbox Code Playgroud)
来源:Android 官方参考
在DrawerLayout类实现Openable。因此,这将简单地工作,而无需您进行任何其他更改。
| 归档时间: |
|
| 查看次数: |
5581 次 |
| 最近记录: |