工具栏导航汉堡图标丢失

我正在寻找一种方式来显示使用抽屉/ DrawerToggle的汉堡包图标,并使用Android中包含的默认图标 在这里输入图像说明

通过设置getSupportActionBar().setDisplayHomeAsUpEnabled(true); 它显示后面的箭头而不是汉堡。 在Stackoverflow上的其他post(如这个或这个 )使用DrawerLayout或自定义drawable。 我找不到在Android源汉堡图标vector或PNG。

你知道我怎么能findAndroid /支持库中的原始汉堡包图标? (或如何显示它)

注意:Vector和png可以在google.com/design网站上find: http //www.google.com/design/spec/resources/sticker-sheets-icons.html#

在我的活动

 mToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(mToolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); mToolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d(LOG_TAG, "navigation clicked"); } }); 

布局文件

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

Styles.xml

 <!-- Base application theme. --> <style name="Theme.AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorPrimary">@color/primaryDef</item> <item name="colorPrimaryDark">@color/primaryDarkDef</item> <item name="colorAccent">@color/primaryDef</item> <!-- Remove the actionbar shadow--> <item name="android:windowContentOverlay">@null</item> </style> 

如果你想使用与棒棒糖相同的抽屉,那么让我告诉你,这不是一个静态图像。 该图像是由名为DrawerArrowDrawableToggle的类实时绘制的。 所以这里没有“汉堡包”图标。

但是,如果你想要没有animation的汉堡包图标,你可以在这里find它:

http://shreyasachar.com/AndroidAssetStudio

您还可以使用http://www.google.com/design/spec/resources/sticker-sheets-icons.html#sticker-sheets-icons-components并下载该包。; 导航图标的名称为ic_menu ,位于navigation文件夹中:

https://github.com/google/material-design-icons/blob/master/navigation/drawable-xxxhdpi/ic_menu_grey600_48dp.png

在这里输入图像说明

要有一个animation汉堡包图标,您应该使用带有ActionBarDrawerToggle DrawerLayout ,并为ActionBarActionBarDrawerToggle启用图标。

例:

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle mDrawerToggle; setSupportActionBar(toolbar); final ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { actionBar.setDisplayHomeAsUpEnabled(true); mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.hello_world, R.string.hello_world) { public void onDrawerClosed(View view) { supportInvalidateOptionsMenu(); //drawerOpened = false; } public void onDrawerOpened(View drawerView) { supportInvalidateOptionsMenu(); //drawerOpened = true; } }; mDrawerToggle.setDrawerIndicatorEnabled(true); drawerLayout.setDrawerListener(mDrawerToggle); mDrawerToggle.syncState(); } 

另外,您需要将这些方法添加到您的活动中:

 @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); mDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mDrawerToggle.onConfigurationChanged(newConfig); } 

我有同样的问题,我发现这里最简单的解决scheme:

appcompatv7-V21-导航抽屉未示出-汉堡包图标

我所要做的就是打电话给:

 mDrawerToggle.syncState(); 

确定隐藏后退箭头使用

 getSupportActionBar().setDisplayHomeAsUpEnabled(false); getSupportActionBar().setHomeButtonEnabled(false); 

然后在web – > hamburger中find汉堡包图标

最后,使用操作栏方法在您的项目中设置此可绘制对象:

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

我有同样的问题。 获取ToolBar ,然后设置导航图标

 final android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar); toolbar.setNavigationIcon(R.drawable.blablabla); 
  Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar); mToolbar.setTitle("title"); setSupportActionBar(mToolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_list); 

这是为我工作的最简单的解决scheme。

ActionBarDrawerToggle有两个types的构造函数。 其中一人以工具栏作为参数。 使用(下面第二个)来获得animation汉堡包。

 ActionBarDrawerToggle(this, mDrawerLayout, R.string.content_desc_drawer_open, R.string.content_desc_drawer_close); ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.content_desc_drawer_open, R.string.content_desc_drawer_close);` //use this constructor 

为此,你只需要写一些行

  DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.setDrawerListener(toggle); toggle.setDrawerIndicatorEnabled(true); toggle.syncState(); 

toggle.setDrawerIndicatorEnabled(真); 如果这是错误的,请将其变为true或删除此行

也许你可以试试这个,但是你会失去箭头和汉堡包图标之间的animation

 @Override protected void onCreate(Bundle arg0) { super.onCreate(arg0); super.setContentView(R.layout.activity_menu_drawer_left); _drawerToggle = new ActionBarDrawerToggle(this, _drawerLayout, R.string.drawer_opened, R.string.drawer_closed) { public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); changeDrawerIconOnDrawerClick(R.drawable.abc_ic_ab_back_mtrl_am_alpha); } /** Called when a drawer has settled in a completely closed state. */ public void onDrawerClosed(View view) { super.onDrawerClosed(view); changeDrawerIconOnDrawerClick(R.drawable.ic_drawer); } }; //to change default icon to hamburger item initially changeDrawerIconOnDrawerClick(R.drawable.ic_drawer); } private void changeDrawerIconOnDrawerClick(int resourceId) { //Drawable icon = ContextCompat.getDrawable(getApplicationContext(), resourceId); Drawable icon = ResourcesCompat.getDrawable(getResources(), resourceId, null); icon.setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_ATOP); _drawerToggle.setDrawerIndicatorEnabled(false); _drawerToggle.setHomeAsUpIndicator(icon); } 

你可以尝试自己制作汉堡包图标。

 <vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportHeight="24.0" android:viewportWidth="24.0"> <path android:fillColor="#ffffff" android:pathData="M3,18h18v-2L3,16v2zM3,13h18v-2L3,11v2zM3,6v2h18L21,6L3,6z" /> </vector> 

然后在你的片段/活动中,

 getSupportActionBar().setHomeAsUpIndicator(R.drawable.as_above); getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

对于其他drawable,这可能有所帮助: https : //github.com/google/material-design-icons/blob/master/navigation/drawable-anydpi-v21/

用你自己的drawablereplace默认的向上箭头

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

只需在您的onCreate方法中添加以下内容,

 if (getSupportActionBar() != null) { getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); } ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, mDrawer, mToolbar, R.string.home_navigation_drawer_open, R.string.home_navigation_drawer_close) { public void onDrawerClosed(View view) { super.onDrawerClosed(view); invalidateOptionsMenu(); } public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); invalidateOptionsMenu(); } @Override public void onDrawerSlide(View drawerView, float slideOffset) { super.onDrawerSlide(drawerView, slideOffset); } }; mDrawer.addDrawerListener(toggle); toggle.syncState(); 

在strings.xml中,

 <string name="home_navigation_drawer_open">Open navigation drawer</string> <string name="home_navigation_drawer_close">Close navigation drawer</string> 

在MyActionBarDrawerToggle中使用这个构造函数:

  public MyActionBarDrawerToggle(AppCompatActivity host, DrawerLayout drawerlayout, SupportToolbar toolbar, int openedResource, int closedResource) : base(host, drawerlayout, toolbar, openedResource, closedResource) { mHostActivity = host; mOpenedResource = openedResource; mClosedResource = closedResource; } 

并在mainActivity中调用此方法(使用AppCompatActivity)

  mDrawerToggle = new MyActionBarDrawerToggle( this, //Host Activity mDrawerLayout, //DrawerLayout mToolbar, //Toolbar Resource.String.openDrawer, //Opened Message Resource.String.closeDrawer //Closed Message ); mDrawerLayout.AddDrawerListener(mDrawerToggle); SupportActionBar.SetHomeButtonEnabled(true); SupportActionBar.SetDisplayShowTitleEnabled(true); mDrawerToggle.DrawerIndicatorEnabled = true; mDrawerToggle.SyncState(); 

onCreate()

  setSupportActionBar(toolbar); ActionBar actionBar = getSupportActionBar(); drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close) { @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); supportInvalidateOptionsMenu(); } @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); supportInvalidateOptionsMenu(); } }; drawerLayout.setDrawerListener(drawerToggle); drawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Backstack.get(MainActivity.this).goBack(); } }); //actionBar.setHomeAsUpIndicator(R.drawable.ic_menu); //getSupportActionBar().setDisplayHomeAsUpEnabled(false); actionBar.setDisplayHomeAsUpEnabled(false); actionBar.setHomeButtonEnabled(true); 

当设置UP导航时:

 private void setupViewsForKey(Key key) { if(key.shouldShowUp()) { drawerToggle.setDrawerIndicatorEnabled(false); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } else { getSupportActionBar().setDisplayHomeAsUpEnabled(false); drawerToggle.setDrawerIndicatorEnabled(true); } drawerToggle.syncState();