更改导航抽屉汉堡图标

Posted

技术标签:

【中文标题】更改导航抽屉汉堡图标【英文标题】:Changing Navigation drawer hamburger icon 【发布时间】:2016-02-02 10:50:40 【问题描述】:

我正在尝试更改 NavigationView 的汉堡菜单图标,但我无法这样做。

这是我目前尝试过的

我有一个完成导航抽屉设置的基本活动。这是相关的代码

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    super.setContentView(R.layout.activity_base_nav);
    setSupportActionBar(toolbar);
    setupDrawer();


private void setupDrawer() 
    mDrawerLayout.setDrawerListener(this);

    mDrawerToggle = new ActionBarDrawerToggle(this,
            mDrawerLayout,
            R.string.open,
            R.string.close);

    mDrawerToggle = new ActionBarDrawerToggle(mContext,
            mDrawerLayout,
            R.string.open,
            R.string.close);

    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (getSupportActionBar() != null) 
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false);
        mDrawerToggle.setDrawerIndicatorEnabled(false);
       mDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_share_48pt_2x);
    
    mDrawerToggle.syncState();

    mNavigationView.setNavigationItemSelectedListener(
            menuItem -> 
                mMenuItem = menuItem.getItemId();
                mDrawerUtil.onNavMenuItemClicked(mMenuItem);
                mDrawerLayout.closeDrawers();
                return true;
            );


@Override 
public void setContentView(int layoutResID) 
    getLayoutInflater().inflate(layoutResID, mContainer);



@Override
protected void onPostCreate(Bundle savedInstanceState) 
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();


@Override
public void onConfigurationChanged(Configuration newConfig) 
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);


@Override
public boolean onOptionsItemSelected(MenuItem item) 
    if (mDrawerToggle.onOptionsItemSelected(item)) 
        return true;
    

    return super.onOptionsItemSelected(item);

但是它似乎对我不起作用。我也尝试在SupportActionBar 上调用setDrawerIndicatorEnabled(false)setHomeAsUpIndicator(R.drawable.ic_share_48pt_2x),但这也不起作用。

【问题讨论】:

【参考方案1】:

下面的代码很适合我,

protected void onCreate(Bundle savedInstanceState) 
    ...
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false);
    toggle.setDrawerIndicatorEnabled(false);
    toggle.setHomeAsUpIndicator(R.drawable.ic_custom_drawer_icon);
    ...

我还必须添加一个工具栏导航点击监听器来监听自定义抽屉图标上的点击事件

protected void onCreate(Bundle savedInstanceState) 
    ...
    toggle.setToolbarNavigationClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            if (drawer.isDrawerOpen(GravityCompat.START)) 
                drawer.closeDrawer(GravityCompat.START);
             else 
                drawer.openDrawer(GravityCompat.START);
            
        
    );
    ...

最后,我可以动态更新图标了

toggle.setHomeAsUpIndicator(R.drawable.ic_new_icon);

【讨论】:

添加 @Override protected void onPostCreate(@Nullable Bundle savedInstanceState) super.onPostCreate(savedInstanceState); nActionBarToggle.syncState(); @Override public void onConfigurationChanged(Configuration newConfig) super.onConfigurationChanged(newConfig); nActionBarToggle.onConfigurationChanged(newConfig); 对我有用 我确实这样做了,但我的切换按钮不可点击。 @Yar 添加了我的解决方案。它包括一个处理点击。 非常感谢,拯救了我的一天【参考方案2】:

就用这个吧:

toolbar.post(new Runnable() 
            @Override
            public void run() 
                Drawable d = ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_launcher, null);
                toolbar.setNavigationIcon(d);
            
        );

您不需要处理已接受答案中的 setToolbarNavigationClickListener。

【讨论】:

出色的解决方案 非常有效的解决方案。但是当它被点击时我怎样才能打开汉堡按钮呢?它没有转向任何地方,只是突出显示背景颜色 WOW 非常适合我,其他方法根本不起作用【参考方案3】:

这对我有用:

override fun onCreate(savedInstanceState: Bundle?) 
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    setSupportActionBar(toolbar)

    val toggle = ActionBarDrawerToggle(
            this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
    //toggle.isDrawerSlideAnimationEnabled = false
    toggle.isDrawerIndicatorEnabled = false
    toggle.setToolbarNavigationClickListener 
        if (drawer_layout.isDrawerOpen(GravityCompat.START))
            drawer_layout.closeDrawer(GravityCompat.START)
        else
            drawer_layout.openDrawer(GravityCompat.START)
    
    toggle.setHomeAsUpIndicator(AppCompatResources.getDrawable(this, R.drawable.ic_android_black_24dp))
    drawer_layout.addDrawerListener(toggle)
    toggle.syncState()
    nav_view.setNavigationItemSelectedListener(this)


override fun onBackPressed() 
    if (drawer_layout.isDrawerOpen(GravityCompat.START)) 
        drawer_layout.closeDrawer(GravityCompat.START)
     else 
        super.onBackPressed()
    


override fun onNavigationItemSelected(item: MenuItem): Boolean 
    when (item.itemId) 
        ...
    
    drawer_layout.closeDrawer(GravityCompat.START)
    return true

【讨论】:

【参考方案4】:

您是否尝试查看文档? http://developer.android.com/training/implementing-navigation/nav-drawer.html

我在里面找到了这段代码:

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, 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()
             
        ;

【讨论】:

问题是我需要动态更改此图标。我需要在此图标上显示通知数量。 @AbhishekBansal 我还需要在汉堡图标上显示通知数量。你做到了吗?【参考方案5】:

带有自定义工具栏

    将您的工具栏设置为 ActionBar。

    然后使用actionBar.setDisplayHomeAsUpEnabled(true);添加homeicon

代码 sn-p:

    Toolbar toolbar  = findViewById(R.id.customFBToolbar);
    setSupportActionBar(toolbar);
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeAsUpIndicator(R.drawable.myham);  // This is the line where you set the drawable
    actionBar.setDisplayShowTitleEnabled(false);

结果:

完整代码:

custom_toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/psyche_toolbar"
    android:layout_
    android:layout_
    android:background="@color/colorPrimaryDark"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    tools:ignore="title"
    tools:targetApi="lollipop">
    <TextView
        android:text="Facebook"
        android:layout_
        android:layout_
        android:textColor="#ffffff"
        android:layout_gravity="center"
        android:textStyle="bold"
        android:textSize="20sp"
        />
    <ImageView
        android:layout_
        android:layout_
        android:layout_gravity="end"
        android:layout_marginEnd="20sp"
        android:elevation="4dp"
        android:src="@android:drawable/ic_dialog_email"/>
</androidx.appcompat.widget.Toolbar>

MainActivity.java

public class MainActivity extends AppCompatActivity 

   @Override
   protected void onCreate(Bundle savedInstanceState) 
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       Toolbar toolbar  = findViewById(R.id.customFBToolbar);
       setSupportActionBar(toolbar);
       ActionBar actionBar = getSupportActionBar();
       actionBar.setDisplayHomeAsUpEnabled(true);
       actionBar.setHomeAsUpIndicator(R.drawable.myham);  // This is the line where you set the drawable
       actionBar.setDisplayShowTitleEnabled(false);

   

   @Override
   public boolean onOptionsItemSelected(MenuItem item) 

       switch (item.getItemId()) 
          case android.R.id.home:
             Toast.makeText(MainActivity.this, "Home button clicked", Toast.LENGTH_SHORT).show();
             return true;

          default:
             return super.onOptionsItemSelected(item);
       
   

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:id="@+id/drawer_layout">
    <include layout="@layout/custom_toolbar"
       android:id="@+id/customFBToolbar"
       android:layout_alignParentTop="true"
       android:layout_
       android:layout_
    />
</RelativeLayout>

【讨论】:

以上是关于更改导航抽屉汉堡图标的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Android 中更改汉堡图标(导航抽屉)

如何更改材料设计导航抽屉中汉堡图标的颜色

如何更改Android中的汉堡包图标(导航抽屉)

Flutter 导航抽屉汉堡图标颜色变化

单击工具栏上的汉堡包图标不会打开导航抽屉

Android汉堡/箭头图标动态改变颜色