2015-04-01 125 views
0

我正在試圖製作一個簡單的導航抽屜,與材料指南相匹配。我沿着the official training。一切運行良好,只有漢堡包圖標上的水龍頭不會打開抽屜。我可以從側面輕掃打開抽屜,只有漢堡包不工作。我已經查了一些其他問題like this one,但沒有任何幫助。我錯過了什麼?Android 5漢堡包不打開抽屜

這裏是我的代碼:

Activity.java:

public class stream extends ActionBarActivity { 
private DrawerLayout mDrawerLayout; 
private ActionBarDrawerToggle mDrawerToggle; 
private String[] mDrawerTitles; 
private ListView mDrawerList; 
private Toolbar mToolbar; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_stream); 

    // Init the Support-toolbar 
    mToolbar = (Toolbar) findViewById(R.id.toolbar); 
    if (mToolbar != null) { 
     setSupportActionBar(mToolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    } 

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, 
      R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    mDrawerLayout.setDrawerListener(mDrawerToggle); 

    // enable ActionBar app icon to behave as action to toggle nav drawer 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    getSupportActionBar().setHomeButtonEnabled(true); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = new MenuInflater(this); 
    inflater.inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    if (mDrawerToggle.onOptionsItemSelected(item)) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

@Override 
protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    mDrawerToggle.syncState(); 
} 

@Override 
public void onBackPressed() { 
    if(mDrawerLayout.isDrawerOpen(Gravity.START|Gravity.LEFT)){ 
     mDrawerLayout.closeDrawers(); 
     return; 
    } 
    super.onBackPressed(); 
} 

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    // Pass any configuration change to the drawer toggle 
    mDrawerToggle.onConfigurationChanged(newConfig); 
    } 
} 

Layout.xml

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="de.square7.gtz.lwenzahn.stream" 
android:background="@color/ColorBackground"> 

<!-- Toolbar --> 
<include layout="@layout/toolbar"/> 

<android.support.v4.widget.DrawerLayout 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <!-- Content View --> 
    <FrameLayout 
     android:id="@+id/content_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingTop="56dp"/> 

    <!-- Drawer --> 
    <ListView 
     android:id="@+id/left_drawer" 
     android:layout_width="270dp" 
     android:layout_height="match_parent" 
     android:focusableInTouchMode="false" 
     android:choiceMode="singleChoice" 
     android:background="#ffffffff" 
     android:layout_gravity="start" 
     android:elevation="8dp" > 
    </ListView> 
</android.support.v4.widget.DrawerLayout> 

+0

在'Android Studio':File - > New Project - > Navigation Drawer。它爲你實現它。 – 2015-04-01 22:32:14

+2

漢堡包是怎麼介入這個的? – aij 2015-04-01 22:56:59

回答

1

您沒有使用DrawerLayout作爲的根佈局活動。你幾乎在那裏,但你看到你如何使用RelativeLayout作爲根(或者至少這是我從推送的XML推斷 - RelativeLayout看起來不像它有一個結束標籤,所以也許你錯過了它)?

NavigationDrawer僅在它是根的時候纔會在「漢堡包」(或圖標)的水龍頭上打開,以便Android知道DrawerLayout包含了所有內容。我的猜測是你所包含的工具欄正在干擾佈局。

+0

是的,它做到了,謝謝! – user3830775 2015-04-01 23:41:37

相關問題