Drawerlayout children views overlapping

2020-02-14 05:58发布

问题:

I've been trying to make a DrawerLayout that has a ViewPager and a LinearLayout attached to the bottom of the DrawerLayout. The problem is that the ViewPager and LinearLayout are overlapping at the top of the view, and all my efforts to move the LinearLayout down have failed so far.

The XML Layout:

<?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.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="0dp" >

            <android.support.v4.view.PagerTabStrip
                android:id="@+id/tabstrip"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </android.support.v4.view.ViewPager>

        <LinearLayout
            android:id="@+id/linear_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_below="@id/base_activity_viewpager"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textAppearance="@android:style/TextAppearance.Medium"
                android:textIsSelectable="false" />
        </LinearLayout>

        <!-- Naviagtion drawer -->

        <ListView
            android:id="@+id/drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#111"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp" />

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

I've tried adding several things to linear_layout:

  1. android:layout_gravity="bottom" (causes an IllegalStateException)
  2. android:layout_below="@id/viewpager"
  3. android:layout_alignparentbottom="true"

But all of these values seem to be ignored, even changing android:layout_width of the LinearLayout is ignored. I think i'm overlooking something obvious but if anyone has suggestions on how to do this it would be greatly appreciated.

回答1:

A drawerlayout is allowed only 2 children, the first is a main content view, the second is the drawer. You need to combine your first two views into a linear layout. http://developer.android.com/training/implementing-navigation/nav-drawer.html