我遇到了问题。我也有一个汉堡包图标和后退箭头。汉堡工作得很好,但不是后箭头。它的工作方式就像一个汉堡包,打开和关闭抽屉菜单。
这是一段代码:
@Override
public void onDrawerClosed(View drawerView) {
hideKeyboard();
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
hideKeyboard();
super.onDrawerOpened(drawerView);
}};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
pager.setVisibility(View.VISIBLE);
tabLayout.setVisibility(View.VISIBLE);
mDrawerToggle.setDrawerIndicatorEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle.syncState();
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbarInner);
toolbar.setNavigationOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
openAndCloseDrawer();
}
});
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
openAndCloseDrawer();
return true;
}
return false;
}
});
public void customizeActionBarWithBack(String string)
{
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbarInner);
toolbar.setTitleTextColor(getResources().getColor(R.color.md_white_1000));
toolbar.setTitle(string);
setDisplayHomeAsUpEnabled(true, R.drawable.abc_ic_ab_back_material);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
toolbar.setNavigationOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
customizeActionBar();
}
});
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId()) {
case android.R.id.home:
recreate();
customizeActionBar();
return true;
}
return false;
}
});
}
哪里有问题?请帮帮我们!
答案 0 :(得分:0)
覆盖onBackPressed
并致电完成。
@Override
public void onBackPressed()
{
finish();
}
答案 1 :(得分:0)
试试这个
@Override
public void onBackPressed()
{
mDrawerLayout.closeDrawers();
}
答案 2 :(得分:0)
Dawid,您也可以像在分段中实现导航抽屉一样:
@Override
public void onBackPressed() {
int count = getFragmentManager().getBackStackEntryCount();
if (count == 0) {
super.onBackPressed();
//additional code
} else {
getFragmentManager().popBackStack();
}
}
和使用接口与活动进行通信的其他选项,在活动中实现接口并在片段中定义它然后你可以通过回调函数来处理它