web-dev-qa-db-fra.com

La barre d'outils Appcompat ne s'affiche pas avec le tiroir de navigation

J'essaie de configurer les éléments suivants dans mon application:

  • Barre d'outils (version Appcompat v7)
  • Tiroir de navigation
  • Pre - Lollipop Appcompat v7 Material theme

J'ai suivi les instructions ici: http://Android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html

Cependant, après avoir déclaré .NoActionBar dans le thème, ainsi que mis la barre d'outils dans la mise en page, ma barre d'outils n'affiche pas . Ce que je finis par obtenir, c'est exactement ce à quoi vous vous attendez en déclarant aucune barre d'action - aucune barre d'action. Voici la mise en page:

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

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

Solution

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

DrawerLayout étend FrameLayout, mais vous le traitez comme un LinearLayout. Vous pouvez soit encapsuler votre balise et le LinearLayout suivant dans un autre LinearLayout, soit déplacer votre balise.

En outre, "fill_parent" est obsolète et correspond à "match_parent", vous devez donc simplement utiliser ce dernier. Vous pouvez également supprimer les attributs xmlns supplémentaires dans votre élément 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"/>
            ...

Votre disposition d'origine n'a pas fonctionné car la barre d'outils était masquée (dans l'ordre z) derrière le LinearLayout.

38
alanv