How to add 3 lines icon in left top corner Android DrawerLayout

泄露秘密 提交于 2019-12-11 07:02:14

问题


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.

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

回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/41749028/how-to-add-3-lines-icon-in-left-top-corner-android-drawerlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!