Appcompat工具栏与导航抽屉不显示

23

我正在尝试在我的应用程序中配置以下内容:

  • 工具栏(Appcompat v7版本)
  • 导航抽屉
  • Pre-Lollipop Appcompat v7材质主题

我按照这里的说明进行操作:http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html

然而,在主题中声明了.NoActionBar,并将工具栏放入布局后,我的工具栏不显示。最终得到的结果与声明没有操作栏时的期望相同-- 没有操作栏。以下是布局:

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

<!-- Toolbar -->
<include layout="@layout/toolbar"/>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout_main"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <Spinner
        android:id="@+id/spinner_main"
        android:visibility="gone"
        android:textAlignment="center"
        android:gravity="center"
        android:layout_gravity="center_horizontal"
        android:entries="@array/error_loading_content_array"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    <FrameLayout
        android:id="@+id/container"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0px"></FrameLayout>

</LinearLayout>

<fragment
    android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:name=".NavigationDrawerFragment"
    tools:layout="@layout/fragment_navigation_drawer"/>

工具栏.xml:

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

在MainActivity.java中:

// Load view/layout
setContentView(R.layout.guidelib_activity_main);

// TODO: Toolbar not showing
mToolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);

解决方案

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearlayout_root_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <LinearLayout
            android:id="@+id/layout_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <!-- Toolbar -->
            <!-- Moved up to new LinearLayout root tag -->
            <!--<include layout="@layout/toolbar"/>-->
            ... 

@tyczj 这是 Android 提供的样式。<style name="ThemeOverlay.AppCompat.ActionBar" parent="Base.ThemeOverlay.AppCompat.ActionBar" /> 即使我更改主题,它仍然不显示我的工具栏。 - user1234
你不应该使用任何带有“actionbar”的主题,因为没有“actionbar”。 - tyczj
1
你的 DrawerLayout 应该有两个视图(在你的情况下是3个)。 - Gabriele Mariotti
@tyczj 噢,好的。我只是在遵循链接中的“样式”建议。 - user1234
@tyczj 向下滚动到“样式”下面,您将看到“深色操作栏”。它描述了如何实现类似深色操作栏的工具栏。 app:theme =“@style/ThemeOverlay.AppCompat.Dark.ActionBar” - user1234
显示剩余3条评论
1个回答

40

DrawerLayout继承自FrameLayout,但您正在将其视为LinearLayout。 您可以将您的<DrawerLayout>标签和随后的<LinearLayout>封装到另一个LinearLayout中,或者您可以移动您的<DrawerLayout>标签。

此外,“fill_parent”已被弃用,并映射到“match_parent”,因此您应该只使用后者。 您还可以删除LinearLayout元素中的附加xmlns属性。

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/layout_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

            <!-- Toolbar -->
            <include layout="@layout/toolbar"/>
            ...

你原来的布局无法工作,因为工具栏(Toolbar)被放置在线性布局(LinearLayout)的后面(z-ordered)。


谢谢,伙计。我把工具栏和之前的LinearLayout都移动到了一个LinearLayout中。现在工具栏可以正常显示了。我注意到导航抽屉现在会显示在工具栏上方。我记得在_Lollipop_中有人说这是有意为之的。然而,我希望导航抽屉仍然像新更新的Google Play应用程序中看到的那样保持在工具栏下面。有什么建议吗? - user1234
1
@user1234 在这种情况下,将工具栏移动到抽屉布局之外。 - Gabriele Mariotti
@GabrieleMariotti 对的。然而,工具栏不会显示在LinearLayout之外,正如我们所发现的。DrawerLayout也是此活动布局中的根标记,因此我也无法将其放置在DrawerLayout之外。 - user1234
2
@GabrieleMariotti 抱歉,是新手错误。我刚醒来还在喝咖啡;) 我将 DrawerLayout 之前的根标签移动到了一个新的 LinearLayout 的根标签中。我先将 Toolbar 放在这个 LinearLayout 中,然后再将 DrawerLayout(包含所有其他内容)放在第二个位置。我会更新我的问题并修复它。 - user1234
你能否请看一下我的问题?我有一个类似的问题,但布局不同:http://stackoverflow.com/questions/27077391/why-does-toolbar-in-appcompat-not-show-when-using-as-an-actionbar - Loolooii

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