简体   繁体   English

更改导航抽屉的图标

[英]Change Icon Of Navigation Drawer

I am having trouble with changing my navigation drawer icon to a custom one. 我将导航抽屉图标更改为自定义图标时遇到问题。 I've currently had to implement the standard drawer icon which has 3 horizontal lines on top, but now I want to replace this with my custom drawer icon. 我目前不得不实现标准的抽屉图标,顶部有3条水平线,但现在我想用我的自定义抽屉图标替换它。

This is how my mDrawerToggle is at the moment: 这就是我的mDrawerToggle目前的情况:

mDrawerToggle=new ActionBarDrawerToggle(this,
    mDrawerLayout,
    R.drawable.app_icon,
    R.string.drawer_open) {
        // My code
    };

Use below code,it's working for V7 ActionBarDrawerToggle 使用下面的代码,它适用于V7 ActionBarDrawerToggle

mDrawerToggle.setDrawerIndicatorEnabled(false);

mDrawerToggle.setHomeAsUpIndicator(R.drawable.your_custom_icon);
 mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
 public void onClick(View v) {
     if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) {
          mDrawerLayout.closeDrawer(GravityCompat.START);
     } else {
         mDrawerLayout.openDrawer(GravityCompat.START);
    }
}
});

Here is the sample code taken from Creating a Navigation Drawer 以下是从创建导航抽屉中获取的示例代码

Activity.class Activity.class

public class MainActivity extends Activity {
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    ...

    public void onCreate(Bundle savedInstanceState) {
        ...

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description */
                R.string.drawer_close  /* "close drawer" description */
                ) {

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

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

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

        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
    }

    @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);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Pass the event to ActionBarDrawerToggle, if it returns
        // true, then it has handled the app icon touch event
        if (mDrawerToggle.onOptionsItemSelected(item)) {
          return true;
        }
        // Handle your other action bar items...

        return super.onOptionsItemSelected(item);
    }

    ...
}

This is main activity 这是主要活动

final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); final 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); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawer,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);

    toggle.setDrawerIndicatorEnabled(false);

    toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            drawer.openDrawer(GravityCompat.START);
        }
    });

    toggle.setHomeAsUpIndicator(R.drawable.menuicon);

You could use this format for your mDrawerToggle : 您可以将此格式用于mDrawerToggle

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
    R.drawable.CUSTOM_ICON, // Navigation menu toggle icon
    R.string.DRAWER_OPEN, // Navigation drawer open description
    R.string.DRAWER_CLOSE // Navigation drawer close description
    )

Change your drawable and make sure it is the same name as the one in the code. 更改您的drawable并确保它与代码中的名称相同。

This is the main layout file 这是主要的布局文件

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- Framelayout to display Fragments -->

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- Listview to display slider menu -->

    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/white"
        android:choiceMode="singleChoice"
        android:divider="@color/black"
        android:dividerHeight="1dp" />

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

This is the main Activity 这是主要活动

DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.menuicon, // nav menu toggle icon
                R.string.app_name, // nav drawer open - description for
                                    // accessibility
                R.string.app_name // nav drawer close - description for
                                    // accessibility
        ) {
            public void onDrawerClosed(View view) {
                // getActionBar().setTitle(mTitle);
                // calling onPrepareOptionsMenu() to show action bar icons
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                // getActionBar().setTitle(mDrawerTitle);
                // calling onPrepareOptionsMenu() to hide action bar icons
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

finally at R.drawable.menuicon (you can give your image id) it will work. 最后在R.drawable.menuicon (你可以给你的图片ID)它会工作。

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

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