Search code examples
androidandroid-layoutandroid-linearlayoutdrawerlayout

layout_weight not working with LinearLayout inside Drawerlayout


I'm struggling with LinearLayout when using DrawerLayout. This is using the Android Studio template for DrawerLayout:

<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=".MyActivity">

<!-- As the main content view, the view below consumes the entire
     space available using match_parent in both dimensions. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:background="#00f"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <LinearLayout
        android:background="#f00"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0" />
    </LinearLayout>
</LinearLayout>

<!-- android:layout_gravity="start" tells DrawerLayout to treat
     this as a sliding drawer on the left side for left-to-right
     languages and on the right side for right-to-left languages.
     If you're not building against API 17 or higher, use
     android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
     the container. -->
<fragment android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:name="com.test.testdrawerlayout.NavigationDrawerFragment"
    tools:layout="@layout/fragment_navigation_drawer" />

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

The preview in Android Studio shows this:

https://i.sstatic.net/UmbxX.jpg

However, when running on my Nexus 5, the following shows:

https://i.sstatic.net/SIzwk.jpg

As you can see, the layout_weight="1" part is not showing at all. When I create a blank project with the inner LinearLayout however, the layout works (with the blue layout taking most of the screen with the red layout at the bottom, just like the preview).

Any ideas would be really appreciated, as I'm completely stumped right now. Thanks in advance.


Solution

  • It appears I was using an older Android SDK platform. Once I went into SDK manager and installed API 19, it worked (with a new project).

    Maybe it was a bug in an older implementation?