我是 android 的新手,我正在尝试向我的项目添加抽屉导航(仅在一个 Activity 中)。 问题是当我添加抽屉时出现错误。为了解决这个问题,我将应用样式更改为 Theme.AppCompat.NoActionBar,但这样做会删除其他 Activity 中的所有其他操作栏。
如果可以解决我的问题,我想在项目中使用另一种样式并仅在该主要 Activity 中删除操作栏。 希望你们能帮助我。谢谢。
这是我的代码:
<style name="MyAppStyle" parent="@style/Theme.AppCompat.NoActionBar">
<item name="colorPrimary">@color/bg_login</item>
<item name="colorPrimaryDark">@color/bg_login</item>
<item name="colorAccent">@color/activity_background</item>
</style>
java代码:
private void setDrawer(){
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
android.support.v7.app.ActionBarDrawerToggle toggle = new android.support.v7.app.ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
//drawer.setDrawerListener(toggle);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
xml文件:
<android.support.v4.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" >
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/bg_login"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
最佳答案
<activity
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
/>
只需将您的主题设置为仅适用于 mainactivity 的主题,然后它就不会在该特定 Activity 上显示 actionbar
关于除主 Activity 之外的所有 Activity 中的 Android 操作栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36796226/