2014-05-19 139 views
3

我越來越瘋狂的Android視圖。我想要一個帶頂部(A)的酒吧和一個酒吧的底部(C)與我的應用程序height="wrap_content"的佈局。中間的完整剩餘空間(B)應該是帶有另一個佈局或TextView或其他內容的內容區域。但我無法得到這個工作。我嘗試了很多佈局,但是當我對B執行android:layout_height="match_parent"時,C消失了。有任何想法嗎?由於事先enter image description here佈局與頂部酒吧,底部酒吧和剩餘空間內容視圖

回答

6

可以使用Android:layout_weight屬性來實現這一點,但你的父容器必須是一個的LinearLayout,在我的例子中,我只使用的LinearLayout作爲父視圖的孩子,但你可以使用另一種類型的觀:

<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:layout_weight="1" 
     android:orientation="vertical"> 

    <LinearLayout 
     android:id="@+id/layout_a" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    </LinearLayout> 

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

    <LinearLayout 
     android:id="@+id/layout_c" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    </LinearLayout> 

</LinearLayout> 

如果你是好奇,想知道它是如何工作......這裏是explanation,好運:)

+0

解決了這個問題。簡單的答案,它的作品。 thx所有答案 –

+1

工作正常,在主線性佈局更改後android:layout_height =「wrap_content」android:layout_height =「match_parent」 – Duque

+0

已修復! :)謝謝@Duque –

0

把內容轉換成一個RelativeLayoutalignParentTopalignParentBottom屬性爲內容Ç並且還belowabove相關屬性內容,如下所示:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
    <!-- create content A and C before --> 
    <LinearLayout 
     android:id="@+id/A" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:orientation="vertical" > </LinearLayout> 
    <LinearLayout 
     android:id="@+id/C" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:orientation="vertical" > </LinearLayout> 
    <!-- create content B regarding the previous ids --> 
    <LinearLayout 
     android:id="@+id/B" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/A" 
     android:layout_above="@id/C" 
     android:orientation="vertical" > </LinearLayout> 
</RelativeLayout> 

您可以嘗試更改B的高度與wrap_content而不是match_parent如果這不起作用。讓我知道這是否有幫助。

+1

是的,這將工作,只是用相同的規格嘲笑了一個視圖,它工作得很好。使用重量的 – OceanLife

0

使用重量:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <FrameLayout 
     android:id="@+id/A" 
     android:layout_width="match_parent" 
     android:layout_height="50dp"> 
     <!-- Your view here or replace FrameLayout --> 
    </FrameLayout> 

    <FrameLayout 
     android:id="@+id/B" 
     android:layout_width="match_parent" 
     android:layout_height="0px" 
     android:layout_weight="1"> 
     <!-- Your view here or replace FrameLayout --> 
    </FrameLayout> 

    <FrameLayout 
     android:id="@+id/C" 
     android:layout_width="match_parent" 
     android:layout_height="50dp"> 
     <!-- Your view here or replace FrameLayout --> 
    </FrameLayout> 

</LinearLayout> 

將高度設置爲0有利於性能,因爲重量無論如何都會改變它。

0
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/top_bar" 
    android:layout_weight="1" 
    android:layout_width="match_parent" 
    android:layout_height="0dp"/> 
<TextView 
    android:id="@+id/middle_area" 
    android:layout_weight="14" 
    android:layout_width="match_parent" 
    android:layout_height="0dp"/> 
    <TextView 
    android:id="@+id/bottom_bar" 
    android:layout_weight="1" 
    android:layout_width="match_parent" 
    android:layout_height="0dp"/> 

android_layout_weight與LinearLayout中結合能爲你做的伎倆。嘗試上面的代碼,並根據您的需要調整android_layout_weight。 在我的例子中,頂欄將佔據屏幕的1/16,底欄將佔屏幕的1/16,中間區域佔屏幕的14/16。您可以將數字更改爲您的自定義需求。