带有图片的折叠式工具栏中的Android主页按钮

5

我希望在我的工具栏中保留主页按钮。我有一个可折叠的工具栏,其中包含一张图片,如果向上滚动,则该图片会消失。在我的其他工具栏中,我使用以下方式实现了工具栏:

getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

但现在它无法正常工作。当工具栏折叠时(图像未显示),我无法看到按钮,当工具栏展开时(图像可见且工具栏已展开)也是如此。

我的工具栏代码:

    Toolbar toolbar = (Toolbar) findViewById(R.id.anim_toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
    collapsingToolbar.setTitle("Awesome");

嗨@user4789408 --- 有找到解决方案吗? - Hardik Amal
2个回答

2

你应该将ToolBar放置在CollapsingToolbarLayout中

<android.support.design.widget.AppBarLayout
        android:layout_height="192dp"
        android:layout_width="match_parent">
    <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
        <android.support.v7.widget.Toolbar
                android:layout_height="?attr/actionBarSize"
                android:layout_width="match_parent"
                app:layout_collapseMode="pin" />
        </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

编辑:http://stackoverflow.com/questions/31505219/android-bug-home-button-in-toolbar-isnt-shown - user4789408
图片怎么样? - Darthcow

-2

您可以通过以下方式添加导航图标:

   final Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
   toolbar.setNavigationIcon(R.drawable.nav_icon);

你可以在onOptionsItemSelected中指定下一步操作:

@Override
public boolean onOptionsItemSelected(final MenuItem item) {
    final int id = item.getItemId();
    if (id == android.R.id.home) {
        //finish();
    }
 //........
}

这是不正确的,因为 onOptionsItemSelected 仅在使用 setActionBar(toolbar) 时起作用。如果您想以这种方式使用它,则必须向工具栏添加侦听器。 - Boy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接