无法使用Material Design添加导航抽屉

时间:2014-11-06 04:45:30

标签: android navigation-drawer material-design

我编写了以下代码,在我的材料设计主题中添加抽屉布局,但未显示。只是工具栏出现了。

MainActivity.java

package com.example.materiald;

import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;

public class MainActivity extends ActionBarActivity {

    ActionBarDrawerToggle mActionBarDrawerToggle;
    DrawerLayout mDrawerLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);

        mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.open_navigation_drawer, R.string.close_navigation_drawer);
        mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

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

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

    <FrameLayout
        android:id="@+id/mainContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

    <!-- Nav drawer -->

    <ListView
        android:id="@+id/drawerList"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="@android:color/white"
        android:divider="@android:color/white"
        android:dividerHeight="8dp"
        android:drawSelectorOnTop="true"
        android:headerDividersEnabled="true" />
</android.support.v4.widget.DrawerLayout>

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res   /android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/myColorPrimary"
android:minHeight="56dp"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

值/ styles.xml

 <style name="AppTheme.Base" parent="Theme.AppCompat"></style>

值/的themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="AppTheme" parent="AppTheme.Base">
    <item name="colorPrimary">@color/myColorPrimary</item>
    <item name="colorPrimaryDark">@color/myColorPrimaryDark</item>
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style>
</resources>

值-V21 /的themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <style name="AppTheme" parent="AppTheme.Base">
  <item name="android:windowContentTransitions">true</item>
  <item name="android:windowAllowEnterTransitionOverlap">true</item>
  <item name="android:windowAllowReturnTransitionOverlap">true</item>
  <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
  <item name="android:windowSharedElementExitTransition">@android:transition/move</item>
  <item name="android:windowTranslucentStatus">true</item>
 </style>
</resources>

1 个答案:

答案 0 :(得分:1)

我错过了我活动中的某些重要陈述。这是完整的活动,其余的保持不变。通过以下更改,我可以添加新的NavigationDrawer

public class MainActivity extends ActionBarActivity {

ActionBarDrawerToggle mDrawerToggle;

DrawerLayout mDrawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);   

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.open_navigation_drawer, R.string.close_navigation_drawer);
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true); //<---- added
    getSupportActionBar().setHomeButtonEnabled(true); //<---- added

}


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

@Override
protected void onPostCreate(Bundle savedInstanceState) { //<---- added
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState(); // important staetment for drawer to identify its state
}

@Override
public void onConfigurationChanged(Configuration newConfig) { //<---- added
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

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