抽屉式导航栏未显示:DrawerLayout导航抽屉未显示

5

我正在尝试在我的工具栏中显示导航抽屉。我认为我已经写好了它所需要的一切,但是在工具栏中没有显示任何内容。只有文本Zoo。没有用于显示导航栏的图标。

我是Android新手,如果我漏掉了什么,这并不奇怪。

这是我的MainActivity.java

package com.example.zoo;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mActionBarDrawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_Layout);
        mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_opened, R.string.drawer_closed){
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                if (getSupportActionBar() != null) {
                    getSupportActionBar().setTitle(R.string.drawer_opened);
                }
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                if (getSupportActionBar() != null) {
                    getSupportActionBar().setTitle(R.string.drawer_closed);
                }
            }
        };

        mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
    }

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

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mActionBarDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        if(mActionBarDrawerToggle.onOptionsItemSelected(item))
            return true;

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

这是我的content_main.xml文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="com.example.gsiradze.zoo.MainActivity"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
   android:id="@+id/toolbar"
   android:layout_height="wrap_content"
   android:layout_width="match_parent"
   android:background="?attr/colorPrimary"
   android:minHeight="?android:attr/actionBarSize"/>

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <ListView
        android:id="@+id/drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/white"/>

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

</LinearLayout>

当然,我有这些字符串名称

<resources>
    <string name="drawer_opened">Select an item</string>
    <string name="drawer_closed">Zoo</string>
</resources>

你在哪里写了打开导航抽屉的代码? - Nigam Patro
@MikeM,你可以将其发布为答案。谢谢伙计。 - gsiradze
@MikeM。实际上两种都是正确的。谢谢 - gsiradze
1个回答

3
问题出在这行代码上:
getSupportActionBar().setDisplayShowHomeEnabled(true);

就您当前的设置而言,应该是:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

您也可以使用 Toggle 的五个参数构造函数,将您的 Toolbar 对象作为第三个参数传递。

mActionBarDrawerToggle = new ActionBarDrawerToggle(this,
                                                   mDrawerLayout,
                                                   toolbar,
                                                   R.string.drawer_opened,
                                                   R.string.drawer_closed) {...};

在这种情况下,您可以省略支持`ActionBar`的`set*()`调用以及对Toggle的`onOptionsItemSelected(item)`检查。如果您的`Toolbar`没有设置为支持`ActionBar`,则这非常有用。然而,文档建议使用四个参数的构造函数,并且如果`Toolbar`设置为支持`ActionBar`,则使用当前设置。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接