2016-04-22 65 views
1

我是Android新手,我試圖在我的項目中添加一個導航抽屜(僅限於一項活動)。 問題是,當我添加抽屜時出現錯誤。爲了解決這個問題,我將我的應用程序樣式更改爲Theme.AppCompat.NoActionBar,但這樣做會刪除其他活動中的所有其他操作欄。除了主要活動之外的所有活動中的Android操作欄

我想在項目中使用另一種風格,並只在該主要活動中刪除操作欄,如果這可以解決我的問題。 希望你們可以幫助我。謝謝。

這裏是我的代碼:

<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" /> 

+0

當我改變了風格@風格/ Theme.AppCompat.Light。 DarkActionBar,我得到這個錯誤:java.lang.IllegalStateException:這個活動已經有一個窗口裝飾提供的操作欄。請勿在您的主題中請求Window.FEATURE_SUPPORT_ACTION_BAR並將windowActionBar設置爲false以代替使用工具欄。 –

+0

「...但這樣做會刪除其他活動中的所有其他操作欄。」 - 在''上設置'NoActionBar'主題。 –

+0

非常感謝。 –

回答

0

您收到此埃羅因爲樣式Theme.AppCompat.Light.DarkActionBar已經包含一個工具欄。所以在這裏你試圖把一個actionBar放在另一個actioBar上,這會導致錯誤。
首先設置一個樣式,其中包含NoActionBar
然後像在每個活動中那樣設置工具欄,並且不要在活動中執行任何不想使用工具欄的任何操作。

另外,如果您正在使用actionBar和navigationDrawer,您可能會對使用Fragments感興趣,這將有助於製作動態UI界面。

+0

非常感謝。 –

1

所有你需要做的就是在清單活動設置不同的主題,

Theme.AppCompat.Light.DarkActionBar您要Actionbar

然後

Theme.AppCompat.NoActionBar到包含Navigation Drawer,也可以作爲AppTheme下活動活動<Application

希望它可以幫助

+0

非常感謝。 –

3
<activity 
      android:theme="@style/Theme.AppCompat.Light.NoActionBar" 
    /> 

只需設置你的主題,只有對mainactivity那麼它不會顯示動作條上的特定活動