导航抽屉打破布局

时间:2013-07-15 16:19:07

标签: android

尝试添加一个破坏我的布局的导航抽屉。主要布局有一些按钮,编辑文本框和Listview。抽屉还有一个列表视图。当我添加抽屉时,主布局上的Listview呈现灰色并占据整个屏幕。然而,抽屉按预期工作。

以下是我的布局文件:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="1000dp"
android:id="@+id/drawer_layout"
tools:context=".Departures"
android:weightSum="3" >

<!-- The main content view -->

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginBottom="12dp"
    android:layout_marginTop="12dp"
    android:gravity="center_horizontal"
    android:text="@string/activity_departures"
    android:textSize="24sp" />

<TextView
    android:id="@+id/div_bar_1"
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:layout_marginBottom="7dp"
    android:background="#666666"
    android:text="@string/empty" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/terminal_1_btn"
        style="@style/navigation_btn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="5dp"
        android:layout_weight="1.5"
        android:background="@drawable/navigation_btn"
        android:minHeight="2dp"
        android:text="@string/terminal_1_btn" />

    <Button
        android:id="@+id/terminal_2_btn"
        style="@style/navigation_btn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="10dp"
        android:layout_weight="1.5"
        android:background="@drawable/navigation_btn"
        android:minHeight="2dp"
        android:text="@string/terminal_2_btn" />
</LinearLayout>

<TextView
    android:id="@+id/div_bar_2"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_marginBottom="7dp"
    android:layout_marginTop="7dp"
    android:background="#999999"
    android:text="@string/empty" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/button1"
        style="@style/navigation_btn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_weight="1"
        android:background="@drawable/navigation_btn"
        android:minHeight="2dp"
        android:text="@string/today_btn" />

    <Button
        android:id="@+id/button2"
        style="@style/navigation_btn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_weight="1"
        android:background="@drawable/navigation_btn"
        android:minHeight="2dp"
        android:text="@string/tomorrow_btn" />

    <Button
        android:id="@+id/button3"
        style="@style/navigation_btn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:layout_weight="1"
        android:background="@drawable/navigation_btn"
        android:minHeight="2dp"
        android:text="@string/variable" />
</LinearLayout>

<TextView
    android:id="@+id/div_bar_3"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_marginBottom="7dp"
    android:layout_marginTop="7dp"
    android:background="#999999"
    android:text="@string/empty" />

<EditText
    android:id="@+id/searchFlights"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:hint="@string/search_flights"
    android:inputType="text"
    android:textSize="14sp" />

<TextView
    android:id="@+id/div_bar_2"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_marginBottom="7dp"
    android:layout_marginTop="7dp"
    android:background="#999999"
    android:text="@string/empty" />

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</ListView>

<!-- The navigation drawer -->

<include layout="@layout/navigation_drawer" />

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

包含文件:

    <!-- The navigation drawer -->
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/left_drawer"
android:layout_width="160dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" >

抽屉列表项目:

   <TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#000"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"/>

根元素(1000dp)的高度仅用于解决仿真器上的显示问题。 救命!我被困在这几个小时......

1 个答案:

答案 0 :(得分:1)

  

根元素(1000dp)的高度仅用于解决模拟器上的显示问题。

我真的不知道你在这里做了什么,因为对于你所描述的目标而言,XML似乎过于复杂。设置导航抽屉的suggested way非常简单明了。我也没有看到你在哪里定义了一个FrameLayout,它将托管你的其余布局(或主要内容)。它确实是最好的方式,可以使XML看起来更清晰。您在FrameLayout中定义主要内容布局(将您的textviews和edittexts包装在linearlayout或其他内容中)并让它们成为所述FrameLayout的后代。然后添加包含导航列表的DrawerLayout。

你在ViewGroups之外有漂浮的子TextViews和EditTexts,这对我来说没有任何意义,为什么那些没有包装在适当的父母中。

示例实现实际上就是您所需要的,因为我使用ViewPagers,Fragments,复合视图等构建了复杂的布局,并且它们与导航抽屉一起工作得很好,因为我遵循了开发人员指南建议的说明。

<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">
<!-- The main content view -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"/>