簡體   English   中英

導航抽屜中斷布局

[英]Navigation Drawer breaking layout

試圖添加一個導航抽屜,這破壞了我的布局。 主布局具有一些按鈕,“編輯文本”框和“列表視圖”。 抽屜也有一個列表視圖。 當我添加抽屜時,主布局上的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" >

抽屜的Listitem:

   <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)僅用於解決模擬器上的顯示問題。 救命! 我在這個上停留了幾個小時...

根元素的高度(1000dp)僅用於解決模擬器上的顯示問題。

我真的不知道您在這里做什么,因為XML對於您所描述的目標而言似乎過於復雜。 建議的設置導航抽屜的方法非常簡單明了。 我也看不到您在哪里定義了一個FrameLayout來托管其余的布局(或主要內容)。 這確實是最好的方法,可以使XML看起來更加簡潔。 您可以在FrameLayout中定義您的主要內容布局(將您的textview和edittexts包裹在linearlayout或其他內容中),並使它們成為所說FrameLayout的后代。 然后添加包含導航列表的DrawerLayout。

您在ViewGroups外部有子TextViews和EditTexts浮動,這對我來說沒有任何意義,為什么它們沒有被適當的父級包裝。

該示例實現實際上是您所需要的,因為我已經使用ViewPagers,Fragments,Composite View等構建了復雜的布局,並且它們都與Navigation Drawer一起很好地工作,因為我遵循了開發人員指南的建議。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM