简体   繁体   English

Appcompatv7-v21导航抽屉不显示汉堡图标

[英]Appcompatv7 - v21 Navigation drawer not showing hamburger icon

I am implementing the lollipop style navigation drawer with latest appcompat support library but the problem is the hamburger icon is never displayed . 我正在使用最新的appcompat支持库实现棒棒糖样式的导航抽屉,但是问题是从未显示汉堡包图标。 Only back icon is shown. 仅显示后退图标。

This is my activity code 这是我的活动代码

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;
import android.view.View;

public class Home extends ActionBarActivity {

private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;

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


private void initViews(){

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


    toolbar.setTitleTextColor(getResources().getColor(android.R.color.white));
    setSupportActionBar(toolbar);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,toolbar ,  R.string.drawer_open, R.string.drawer_close) { 

        /** Called when a drawer has settled in a completely closed state. */ 
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            //getActionBar().setTitle(mTitle);
            //invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        } 

        /** Called when a drawer has settled in a completely open state. */ 
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            //getActionBar().setTitle(mDrawerTitle);
            //invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        } 
    }; 


    // Set the drawer toggle as the DrawerListener 
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    getSupportActionBar().setHomeButtonEnabled(true); 

 }
}

This is my styles file 这是我的样式文件

 <resources>
 <!-- Application theme. -->
<style name="Theme.Test" parent="@style/Theme.AppCompat.Light">

    <!-- customize the color palette -->
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
    <item name="windowActionBar">false</item>
    <item name="drawerArrowStyle">@style/Theme.Test.DrawerArrowStyle</item>
</style>

<style name="Theme.Test.DrawerArrowStyle" parent="@style/Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">true</item>
    <item name="color">@android:color/white</item>
</style>

The layout file 布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

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

    <!-- The main content view -->

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The navigation drawer -->

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#111"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>

</RelativeLayout>

导航抽屉显示后退按钮

导航抽屉显示后退按钮

In both cases only back arrow is shown , i have read many posts but nothing seems to make a difference . 在这两种情况下,只显示了向后箭头,我阅读了很多文章,但似乎没有什么改变。 Any help would be appreciated. 任何帮助,将不胜感激。

你需要打电话

mDrawerToggle.syncState();

Make sure that you're importing the correct drawer toggle. 确保您导入了正确的抽屉开关。

When I imported the v4 version I had the arrow (below). 当我导入v4版本时,我有箭头(如下)。

import android.support.v4.app.ActionBarDrawerToggle;

Changing it to this (below, v7) fixed my issue. 将其更改为此(以下,v7)解决了我的问题。

import android.support.v7.app.ActionBarDrawerToggle;

Make sure that you call 确保您打电话

mDrawerToggle.syncState();

AFTER calling 通话后

getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
getSupportActionBar().setHomeButtonEnabled(true); 

When using the ActionBarDrawerToggle, you must call it during onPostCreate() and onConfigurationChanged() 使用ActionBarDrawerToggle时,必须在onPostCreate()和onConfigurationChanged()期间调用它

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

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

Since my NavigationDrawer was extending a Fragment not an Activity I was not able to override postCreate. 由于我的NavigationDrawer扩展了一个不是Activity的Fragment,所以我无法覆盖postCreate。 The below is what I did. 以下是我所做的。

   ActionBar actionBar = getActionBar();
   actionBar.setDisplayHomeAsUpEnabled(true); // this sets the button to the    back icon
   actionBar.setHomeButtonEnabled(true); // makes it clickable
   actionBar.setHomeAsUpIndicator(R.drawable.ic_drawer);// set your own icon

Hope it helps! 希望能帮助到你!

Dont forget to override onOptionsItemSelected method and check if ctionBarDrawerToggle was clicked, in this case return true otherwise the activity will be finished. 不要忘记重写onOptionsItemSelected方法,并检查是否单击了ctionBarDrawerToggle,在这种情况下,返回true,否则活动将结束。

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

You can simply use this: 您可以简单地使用:

// Defer code dependent on restoration of previous instance state.
mDrawerLayout.post(new Runnable() {
    @Override
    public void run() {
        mDrawerToggle.syncState();
        getActionBar().setHomeAsUpIndicator(R.drawable.ic_drawer);
    }
});

mDrawerToggle.syncState() did not work for me, but I eventually got it to work with: mDrawerToggle.syncState()对我不起作用,但最终我将其与以下文件一起使用:

getSupportActionBar().setHomeAsUpIndicator(R.drawable.hamburger_icon);

I was, however, not using a Toolbar. 但是,我没有使用工具栏。

While including ActionBarDrawerToggle, make sure to use the post method: 在包含ActionBarDrawerToggle时,请确保使用post方法:

mDrawerLayout.post(new Runnable() {
   @Override
   public void run() {
       mDrawerToggle.syncState();
   }
});

I also got similar problem, in my case problem was, when initiating actionbartoggle, I was not passing valid toolbar argument (toolbar was initialized later), without a proper, non-null toolbar, ActionBarToggle will fail to create a hamburger icon. 我也遇到了类似的问题,就我而言,问题是,当启动actionbartoggle时,我没有传递有效的工具栏参数(工具栏稍后进行了初始化),如果没有正确的非空工具栏,ActionBarToggle将无法创建汉堡图标。

actionBarToggle = ActionBarDrawerToggle(this, mDrawer, toolbar, 
R.string.drawer_open, R.string.drawer_close);

you can call syncState() from your Activity's onPostCreate to synchronize the indicator with the state of the linked DrawerLayout after onRestoreInstanceState has occurred. 您可以在onRestoreInstanceState发生后,从Activity的onPostCreate调用syncState()将指示器与链接的DrawerLayout的状态同步。

@Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

Also ActionBarDrawerToggle can be used directly as a DrawerLayout.DrawerListener, or if you are already providing your own listener, call through to each of the listener methods from your own. 另外,ActionBarDrawerToggle也可以直接用作DrawerLayout.DrawerListener,或者,如果您已经提供了自己的侦听器,则可以通过自己的方法调用每个侦听器方法。

private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
  .
  .
  .
  .
mDrawerLayout.setDrawerListener(mDrawerToggle);

    mDrawerLayout.post(new Runnable() {
        @Override
        public void run() {
            mDrawerToggle.syncState();
        }
    });

This works for me. 这对我有用。 I have extended AppCompatActivity instead of ActionBarActivity. 我已经扩展了AppCompatActivity而不是ActionBarActivity。

mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,null, 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);
        mActionBarDrawerToggle.syncState();
    }

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

    }
};

Navigation drawer was not showing when clicking action bar menu. 单击操作栏菜单时,导航抽屉未显示。 This fixed it for me. 这为我解决了。

   @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
      //add your switch statement


        return super.onOptionsItemSelected(item);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM