我希望工具栏中的后退图标在左侧,并返回到上一页。工具栏中的菜单图标应位于右侧,并显示导航抽屉。
我已经有:
我的代码:
@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");
答案 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);
}
}