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

Rav*_*avi 94 android android-layout android-support-library android-navigation

我正在使用最新的appcompat支持库实现棒棒糖风格的导航抽屉,但问题是汉堡包图标永远不会显示.仅显示后退图标.

这是我的活动代码

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); 

 }
}
Run Code Online (Sandbox Code Playgroud)

这是我的样式文件

 <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>
Run Code Online (Sandbox Code Playgroud)

布局文件

<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>
Run Code Online (Sandbox Code Playgroud)

显示后面按钮的导航抽屉

显示后面按钮的导航抽屉

在这两种情况下只显示了后退箭头,我已阅读了很多帖子,但似乎没有任何区别.任何帮助,将不胜感激.

Ped*_*ira 145

你需要打电话

mDrawerToggle.syncState();
Run Code Online (Sandbox Code Playgroud)

  • 刚刚初始化`ActionBarDrawerToggle`之后 (14认同)
  • 我猜里面onDrawerClosed()和onDrawerOpened() (2认同)

Don*_*ker 19

确保您导入正确的抽屉切换.

当我导入v4版本时,我有箭头(下面).

import android.support.v4.app.ActionBarDrawerToggle;
Run Code Online (Sandbox Code Playgroud)

将其更改为此(下面,v7)修复了我的问题.

import android.support.v7.app.ActionBarDrawerToggle;
Run Code Online (Sandbox Code Playgroud)


luk*_*kle 14

确保你打电话

mDrawerToggle.syncState();

打电话之后

getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
getSupportActionBar().setHomeButtonEnabled(true); 
Run Code Online (Sandbox Code Playgroud)


Sat*_*esh 13

使用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);
    }
Run Code Online (Sandbox Code Playgroud)


小智 9

由于我的NavigationDrawer正在扩展Fragment而不是Activity,因此我无法覆盖postCreate.以下是我的所作所为.

   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
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!


小智 6

不要忘记覆盖onOptionsItemSelected方法并检查是否单击了ctionBarDrawerToggle,在这种情况下返回true,否则活动将完成.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
Run Code Online (Sandbox Code Playgroud)

  • 单行:`return actionBarDrawerToggle.onOptionsItemSelected(item)|| super.onOptionsItemSelected(项目)` (3认同)

小智 5

您可以简单地使用:

// 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);
    }
});
Run Code Online (Sandbox Code Playgroud)