2013-03-27 68 views
0

我想做一些非常簡單的事情。我想要一個頂部有一個微調器的佈局,接着是一個列表視圖,然後是最底部的一個線性佈局,它包裝了一些按鈕。我希望列表視圖能夠展開,以填充微調框和按鈕之間的空間,無論窗口有多大。我一直在嘗試用線性佈局封裝所有三個元素,並且我已經嘗試了可以​​考慮的Layout_Height的每個Wrap Content和Fill Parent的組合,但除非我硬編碼列表視圖Layout_Height來說出300 dip,這些按鈕被推送關閉屏幕。我知道必須有一個簡單的方法來做到這一點,但我在我的智慧結局。我嘗試了所有我能想到的。簡單的Android佈局問題

下面是與硬編碼高度一起使用的代碼。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<Spinner 
    android:id="@+id/fileType" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 

<ListView 
    android:id="@+id/android:list" 
    android:layout_width="fill_parent" 
    android:layout_height="300dip" /> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_gravity="bottom" 
    android:gravity="bottom" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/ManageFiles_DeleteItem" 
      android:layout_width="fill_parent" 
      android:layout_height="40dip" 
      android:layout_margin="5dip" 
      android:layout_weight="1" 
      android:text="Delete item" /> 

     <Button 
      android:id="@+id/ManageFiles_DeleteAll" 
      android:layout_width="fill_parent" 
      android:layout_height="40dip" 
      android:layout_margin="5dip" 
      android:layout_weight="1" 
      android:text="Delete all" /> 

     <Button 
      android:id="@+id/ManageFiles_DisplayItem" 
      android:layout_width="fill_parent" 
      android:layout_height="40dip" 
      android:layout_margin="5dip" 
      android:layout_weight="1" 
      android:text="Display item" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/ManageFiles_OKcustom" 
      android:layout_width="fill_parent" 
      android:layout_height="40dip" 
      android:layout_margin="10dip" 
      android:layout_weight="1" 
      android:text="OK" /> 

     <Button 
      android:id="@+id/ManageFiles_CancelCustom" 
      android:layout_width="fill_parent" 
      android:layout_height="40dip" 
      android:layout_margin="10dip" 
      android:layout_weight="1" 
      android:text="Cancel" /> 
    </LinearLayout> 
</LinearLayout> 

`

+0

什麼是你的設備的總高度,你需要什麼高度爲每個微調,列表視圖,LinearLayout中 – 2013-03-27 04:20:00

+0

@Arju,你在想什麼樣的框架呢?你真的在想Android設備嗎? – 2013-03-27 04:43:06

回答

0

使用以下

android:weightSum="Your total length" //in your main layout 

android:layout_weight="" //in each of your listview,spinner,linearlayout 

例如:如果u需要爲所有3個要素使用

android:weightSum="3" 

然後在

Spinner 
android:layout_weight="1" 
/> 

ListView 
android:layout_weight="1" 
/> 

LinearLayout 
android:layout_weight="1" 
/> 
等於空間0
0

使用重量,給列表視圖兩個權重,每個權重爲微調和包含按鈕的底部佈局,然後可以改變重量並查看哪些更適合您。

+0

這似乎沒有工作。當名單被填充時,它漂浮在頂部,並不是很高。底部的按鈕佈局走到了屏幕的中間。爲什麼這麼難??? – Don 2013-03-27 05:49:47

+0

你忘了告訴我一個重要的信息!爲此,您必須將所有三個元素的layout_height設置爲0px。現在它工作了! – Don 2013-03-27 06:00:03

+0

你必須這樣做,實際上我曾嘗試編輯你發佈的佈局並將其發回,但是有一些與stackoverflow編輯器的格式問題,所以我不能回發 – user2041902 2013-03-27 12:42:55

1

您可以嘗試簡單的東西作爲

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

<Spinner 
    android:id="@+id/spinner1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" /> 

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@id/spinner1" 
    android:layout_above="@+id/button1" > 
</ListView> 

<Button 
    android:id="@id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:text="Button1" /> 

<Button 
    android:id="@id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:text="Button2" /> 

的技巧是使用RelativeLayout的,而不是LinearLayout中。

+0

謝謝,我不知道相對佈局。我用另一個建議來使用layout_weight,它解決了這個問題,但我想我現在會嘗試使用Relative Layout來查看它是否有效,然後我將把它放在我的曲目中。 – Don 2013-03-27 06:03:44

0

試試像這樣它會適合所有屏幕尺寸。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<LinearLayout 
    android:id="@+id/ftr_btn" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:gravity="bottom" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/ManageFiles_DeleteItem" 
      android:layout_width="fill_parent" 
      android:layout_height="40dip" 
      android:layout_margin="5dip" 
      android:layout_weight="1" 
      android:text="Delete item" /> 

     <Button 
      android:id="@+id/ManageFiles_DeleteAll" 
      android:layout_width="fill_parent" 
      android:layout_height="40dip" 
      android:layout_margin="5dip" 
      android:layout_weight="1" 
      android:text="Delete all" /> 

     <Button 
      android:id="@+id/ManageFiles_DisplayItem" 
      android:layout_width="fill_parent" 
      android:layout_height="40dip" 
      android:layout_margin="5dip" 
      android:layout_weight="1" 
      android:text="Display item" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/ManageFiles_OKcustom" 
      android:layout_width="fill_parent" 
      android:layout_height="40dip" 
      android:layout_margin="10dip" 
      android:layout_weight="1" 
      android:text="OK" /> 

     <Button 
      android:id="@+id/ManageFiles_CancelCustom" 
      android:layout_width="fill_parent" 
      android:layout_height="40dip" 
      android:layout_margin="10dip" 
      android:layout_weight="1" 
      android:text="Cancel" /> 
    </LinearLayout> 
</LinearLayout> 

<Spinner 
    android:id="@+id/fileType" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" /> 

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/ftr_btn" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/fileType" > 
</ListView> 

+0

這是我正在尋找的解決方案。它完美的作品。謝謝。 – Don 2013-03-28 05:13:29

+0

@唐永遠歡迎您。 – Yugesh 2013-03-28 13:28:58