菜单选项未显示在可折叠工具栏上

10

我正在尝试使用可折叠的工具栏布局和菜单选项,但是每当我在下面的代码中设置图像视图源时,该图像会覆盖菜单和返回按钮。菜单选项是存在的,因为我可以点击它们,只是被图像隐藏了。有人知道如何解决吗?

XML布局

    <android.support.design.widget.AppBarLayout android:id="@+id/app_bar"
        android:layout_width="match_parent" android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/toolbar_layout"
            android:layout_width="match_parent" android:layout_height="match_parent"
            android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed" app:toolbarId="@+id/toolbar">

            <android.support.v7.widget.Toolbar android:id="@+id/detail_toolbar"
                android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                android:fitsSystemWindows="true"/>

            <ImageView
                android:src="@drawable/background"
                app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                android:minHeight="100dp"
                android:fitsSystemWindows="true"/>

        </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

这是否在 CoordinatorLayout 中? - Simon
是的,除了菜单图标不显示之外,一切都正常运作。 - Carl
3个回答

9
请尝试以下操作:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true" >

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:toolbarId="@+id/toolbar" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_collapseMode="parallax"
                android:background="@drawable/background"
                android:fitsSystemWindows="true"
                android:minHeight="100dp"
                android:scaleType="centerCrop" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/detail_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                android:fitsSystemWindows="true"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

如果有任何问题,请留下评论。

祝你好运!


请尝试将以下内容添加到您的工具栏中:android:gravity="center_vertical" - Netero
8
你能描述一下你在原帖的代码和你回答中的代码之间做了哪些修改吗? - Michael Fulton
3
将工具栏移至末尾。 工具栏将成为可折叠工具栏布局的最后一个子项。 - thanhnd

7

从我的经验来看,这说明您可能忘记了在活动中添加以下行:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(toolbar);


这里发生了同样的事情。我在片段中使用工具栏,但忘记设置这段代码。建议遇到相同问题的人首先尝试这行代码,因为它简短且验证速度快。 - Anthonyeef

4
尝试向工具栏添加collapseMode属性…像这样。它可以固定工具栏。
<android.support.v7.widget.Toolbar


    app:layout_collapseMode="pin" />

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