Using AppCompat Toolbar with FrameLayout

So my XML looks like this:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_height="56dp"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary" />

    <FrameLayout
        android:id="@+id/main_frag"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- ListView here -->

</android.support.v4.widget.DrawerLayout>

      

What happens, even though I set my height explicitly to 56dp, toolbar

acts like match_parent

and displays the full height of the screen? Is there a better way to do this?

Or should I position the toolbar towards the layouts that mine FragementTransaction

fills in FrameLayout

? It doesn't seem to be efficient because I have several of them.

+3


source to share


1 answer


DrawerLayout

displays two child views: the first for the main content and the second for the drawer: both are always set to match_parent

. Therefore yours Toolbar

and FrameLayout

should be wrapped with a vertical LinearLayout

, which is set match_parent

according to the canonical example from the creator of AppCompat:



<!-- The important thing to note here is the added fitSystemWindows -->
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <!-- Your normal content view -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!-- We use a Toolbar so that our drawer can be displayed
             in front of the action bar -->
        <android.support.v7.widget.Toolbar  
            android:id="@+id/my_toolbar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:minHeight="?attr/actionBarSize"
            android:background="?attr/colorPrimary" />

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

    </LinearLayout>

    <!-- Your drawer view. This can be any view, FrameLayout
         is just an example. As we have set fitSystemWindows=true
         this will be displayed under the status bar. -->
    <FrameLayout
        android:layout_width="304dp"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:fitsSystemWindows="true">

        <!-- ListView here -->

    </FrameLayout>

</android.support.v4.widget.DrawerLayout>

      

+8


source







All Articles