2017-10-13 33 views
0

我無法定位操作欄,爲什麼顯示兩次。 應該只有一個,nav_drawer,應用程序名稱和設置按鈕在右側。Double ActionBar/Toolbar

<?xml version="1.0" encoding="utf-8"?> 
<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"> 

    <android.support.design.widget.CoordinatorLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     tools:context=".MainActivity"> 

     <android.support.design.widget.AppBarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:theme="@style/AppTheme.AppBarOverlay"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="?attr/colorPrimary" 
       app:popupTheme="@style/AppTheme.PopupOverlay" /> 

     </android.support.design.widget.AppBarLayout> 

     <include layout="@layout/content_main" /> 

    </android.support.design.widget.CoordinatorLayout> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_main" 
     app:menu="@menu/activity_main_drawer" /> 

</android.support.v4.widget.DrawerLayout> 

在MainActivity中,我只設置了包含工具欄和main_content的佈局。

它看起來像在預覽

enter image description here

在模擬器

enter image description here

setContentView(R.layout.nav_drawer_main); 
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 

//getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
     this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
drawer.setDrawerListener(toggle); 
toggle.syncState(); 
+1

你可能不會使用你的'Activity'適當父主題添加NoActionBar在style.xml

<style name="AppTheme.NoActionBar"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style> 

設置工具欄。你的主題是基於什麼? –

+1

使用'NoActionBar'主題來符合你的風格。像'parent =「Theme.AppCompat.NoActionBar」'或'parent =「Theme.AppCompat.Light.NoActionBar」'。 – Jerrol

+0

是的,它是風格中的基本應用主題。它是:<! - Base application theme。 - >