Подтвердить что ты не робот

Панель инструментов Appompat не отображается с помощью навигационного ящика

Я пытаюсь настроить следующее в своем приложении:

  • Панель инструментов (версия 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"/>

Toolbar.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"/>-->
            ... 
4b9b3361

Ответ 1

DrawerLayout расширяет FrameLayout, но вы рассматриваете его как LinearLayout. Вы можете либо обернуть свой тег, и следующий LinearLayout в другой LinearLayout, или вы можете переместить свой тег.

Кроме того, "fill_parent" устарел и отображает "match_parent", поэтому вы должны просто использовать последний. Вы также можете удалить дополнительные атрибуты xmlns в элементе LinearLayout.

<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"/>
            ...

Исходный макет не работал, потому что панель инструментов была скрыта (z-упорядочена) за LinearLayout.