How to add 3 lines icon in left top corner Android

2019-08-01 03:34发布

I'am writing new app and I want to change my drawerLayout icon look to typical 3 horizontal lines. If I click on it, Icon should change his look to arrow. Now I have an arrow icon all the time.

How should It looks like when drawer is closed

How should It looks like when drawer is opened

package pl.nieruchalski.scrumfamily;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.PersistableBundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends Activity {

    private ActionBarDrawerToggle drawerToggle;
    private DrawerLayout drawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.openDrawer, R.string.closeDrawer);
        drawerLayout.addDrawerListener(drawerToggle);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
    }

    @Override
    public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onPostCreate(savedInstanceState, persistentState);
        drawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        drawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(drawerToggle.onOptionsItemSelected(item))
            return true;
        return super.onOptionsItemSelected(item);
    }
}

2条回答
Juvenile、少年°
2楼-- · 2019-08-01 03:46

try this in onCreate():

 getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false);
    drawerToggle.setDrawerIndicatorEnabled(false);
    drawerToggle.setHomeAsUpIndicator(R.drawable.ic_custom_drawer_icon); //set your horizontal line icon
    drawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
            if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
                drawerLayout.closeDrawer(GravityCompat.START); 
                drawerToggle.setHomeAsUpIndicator(R.drawable.ic_custom_drawer_icon); // drawer closed-reset icon
            } else {
                //open drawer
                drawerLayout.openDrawer(GravityCompat.START);
                drawerToggle.setHomeAsUpIndicator(R.drawable.ic_new_icon); // set your back icon
            }
        }
    });
查看更多
相关推荐>>
3楼-- · 2019-08-01 03:56

You're overriding the wrong onPostCreate() method, so drawerToggle.syncState(); never gets called, and the toggle never puts its own icon on the ActionBar.

You need to override the onPostCreate() method with only the Bundle parameter; no PersistableBundle parameter.

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    drawerToggle.syncState();
}
查看更多
登录 后发表回答