如何更改默认汉堡包图标?

时间:2016-04-19 11:33:13

标签: android android-fragments android-studio hamburger-menu

我想改变汉堡包图标。请给我一个解决方案。 MainActivity课程如下:

    public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {


        private Toolbar mToolbar;


 DrawerLayout mDrawerLayout;
    private FragmentDrawer drawerFragment;
    RelativeLayout relativeid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        relativeid = (RelativeLayout) findViewById(R.id.relativeid);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawerFragment = (FragmentDrawer) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
        drawerFragment.setUp(R.id.fragment_navigation_drawer, mDrawerLayout, mToolbar);
        drawerFragment.setDrawerListener(this);
        displayView(0);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        if (id == R.id.action_search) {
            Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onDrawerItemSelected(View view, int position) {
        displayView(position);
    }

    private void displayView(int position) {
        Fragment fragment = null;
        String title = "";
        switch (position) {
            case 0:
                fragment = new HomeFragment();
                title = "";
                break;
            case 1:
                fragment = new FriendsFragment();
                title = "";
                break;
            case 2:
                fragment = new MessagesFragment();
                title = "";
                break;
            default:
                break;
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container_body, fragment);
            fragmentTransaction.commit();
            // set the toolbar title
            getSupportActionBar().setTitle(title);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您需要创建自定义工具栏并手动充气。

private Toolbar toolbar;
toolbar = (Toolbar) findViewById(R.id.toolbar); 

if (toolbar != null) { 
    setSupportActionBar(toolbar); 
    toolbar.setNavigationIcon(R.drawable.ic_drawer); 

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

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

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

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

这应该有用!!