显示导航抽屉的菜单项和返回的后退箭头

时间:2018-08-10 11:11:34

标签: java android navigation-drawer toolbar

我希望工具栏中的后退图标在左侧,并返回到上一页。工具栏中的菜单图标应位于右侧,并显示导航抽屉。

我已经有:

enter image description here

我的代码

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mToolbar=(android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);

    //drawerlayout
    mDrawerLayout=(DrawerLayout) findViewById(R.id.activity_main3);
    ActionBarDrawerToggle mToggle=new ActionBarDrawerToggle(this,mDrawerLayout,mToolbar,R.string.open,R.string.close);
    mDrawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setTitle("NOSTRILS");

1 个答案:

答案 0 :(得分:0)

通过对代码进行大量更改解决了该问题:

public class MainActivity extends AppCompatActivity {
    private DrawerLayout drawerLayout;
    private Toolbar toolbar;
    private EndDrawerToggle drawerToggle;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        /* Toolbar */
        getSupportActionBar().setTitle("Nostrils");
        toolbar.setTitleTextColor(0xFFFFFFFF);
        // Back icon
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        initNavigationDrawer();
    }


        drawerLayout = (DrawerLayout)findViewById(R.id.activity_main3);

        drawerToggle = new EndDrawerToggle(this,
                drawerLayout,
                toolbar,
                R.string.open,
                R.string.close);

        drawerLayout.addDrawerListener(drawerToggle);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();
    }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here.
    int id = item.getItemId();

        // Back icon clicked
        Intent i = new Intent(MainActivity.this, Main2Activity.class);
        startActivity(i);
        finish();

    return super.onOptionsItemSelected(item);
}


}