问题
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