Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 带图像的折叠工具栏中的安卓主页按钮_Android_Scroll_Toolbar_Material Design_Collapse - Fatal编程技术网

Android 带图像的折叠工具栏中的安卓主页按钮

Android 带图像的折叠工具栏中的安卓主页按钮,android,scroll,toolbar,material-design,collapse,Android,Scroll,Toolbar,Material Design,Collapse,我希望保持主页按钮在我的工具栏上。我有一个正在折叠的工具栏,上面有一个图像,如果向上滚动就会消失。在我的另一个工具栏中,我使用以下工具实现了工具栏: getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); 但现在不行了。当工具栏折叠(图像未显示)和工具栏打开(图像可见且工具栏展开)时,我都看不到该按钮 我的工具栏代码: Toolbar

我希望保持主页按钮在我的工具栏上。我有一个正在折叠的工具栏,上面有一个图像,如果向上滚动就会消失。在我的另一个工具栏中,我使用以下工具实现了工具栏:

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");

您应该将工具栏置于折叠工具栏布局中

<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>

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

   final Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
   toolbar.setNavigationIcon(R.drawable.nav_icon);
您可以在OnOptions ItemSelected中指定操作,如下所示:

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

编辑:图像如何?您好@user4789408---找到解决方案了吗?这是不正确的,因为
onOptionsItemSelected
仅在使用
setActionBar(工具栏)
时有效。如果要以这种方式使用工具栏,必须向工具栏添加侦听器