Android 5 Hamburger doen打开抽屉

时间:2015-04-01 22:20:31

标签: android navigation-drawer android-5.0-lollipop

我正在尝试制作一个简单的导航抽屉,符合材料指南。我跟着the official training。一切运行良好,只需点击汉堡包图标就无法打开抽屉。我可以从侧面轻扫打开抽屉,只有汉堡不能正常工作。我已经查询了其他一些问题like this one,但没有任何帮助。我错过了什么?

这是我的代码:

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>

1 个答案:

答案 0 :(得分:1)

您没有使用DrawerLayout作为活动的根布局。你几乎就在那里,但你看到你如何使用RelativeLayout作为根(或者至少这是我从发布的xml推断 - RelativeLayout看起来不像它有一个结束标记,所以也许你只是错过了它)?

只有当它是根时才会打开“汉堡包”(或图标),以便Android知道DrawerLayout包含所有内容。我的猜测是你所包含的工具栏会干扰布局。