AppCompat工具栏未显示

11
在主题中声明.NoActionBar,并将toolbar放在布局中后,我的toolbar没有显示。最终得到的结果就是你所期望的没有操作栏。以下是布局代码:

activity_home.xml:

<include
    layout="@layout/app_bar_home"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_home"
    app:menu="@menu/activity_home_drawer"/>

app_bar_home.xml

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"/>

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

<include layout="@layout/content_home"/>

:这是一个空段落标签,用于在HTML文档中创建一个新的段落。

5
您需要提供完整的XML结构,而不仅仅是片段。activity_home的根视图组类型是什么?是LinearLayout吗?您在这里遇到麻烦的最可能原因是app_bar_homenav_view都将其height属性设置为match_parent,因此其中一个与父级匹配并将另一个推出屏幕。 - Tim Malseed
我不是很理解。在声明.NoActionBar之后,为什么你期望你的工具栏会被显示出来?我总是这样做来移除我的工具栏。 - user12465043
1个回答

11

在您的活动中,如果您尚未这样做,您需要初始化您的工具栏。

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

6
也请查看https://dev59.com/018d5IYBdhLWcg3wt0Gy,这是一个更完整的答案。上述解决方案需要修复样式才能正常工作。 - StarWind0

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