后退按钮不使用导航视图和工具栏显示

时间:2015-07-09 18:58:28

标签: android navigation androiddesignsupport

我有一个具有以下层次结构的应用程序:

MainActivity (Shows list of dates) 
    | 
ViewPagerFragment (Shows list of children for those dates)
    | 
ChildFragment (Detail View)

我正在尝试从设计支持库中实现导航视图,但是无法让工具栏上的实际导航工作。

这是主要活动工具栏:

main activity navigation

以下是从主活动导航后的ViewPagerFragment,请注意没有后退按钮......

child activity navigation

这是所需的工具栏: desired toolbar

我使用以下代码添加片段:

    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.content_frame, fragment, fragment.getClass().getName())
            .addToBackStack(fragment.getClass().getName())
            .commitAllowingStateLoss();

以下是与活动启动相关的代码:

protected void setupActionBar() {
    setSupportActionBar(toolbar);

    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
}

我的导航抽屉设置:

protected void setupNavigationDrawer() {
    navigationView.setNavigationItemSelectedListener(this);
    actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawerLayout.setDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.syncState();
}

点击设备上的硬件返回按钮可以正确导航。我似乎无法让抽屉上的后箭头显示出来......有什么建议吗?

2 个答案:

答案 0 :(得分:3)

要禁用导航抽屉图标并显示其他图标,您需要在ActionBarDrawerToggle上致电setDrawerIndicatorEnabled(false)

您可能还需要致电setHomeAsUpIndicator()来指定您要使用的图标,而不是"汉堡包"图标。

答案 1 :(得分:1)

您是否尝试过活动的ActionBar?

onViewCreated中的

Toolbar mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
((Activity) getActivity()).setSupportActionBar(mToolbar);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
相关问题