2012-03-14 125 views
1

我正在開發2.2版本的Android應用程序。Android應用程序佈局

我的應用程序必須甲肝這樣的結構:

[ Spinner here (fixed height) ]

[ ListView (not-fixed height) ]

[ ImageView (fixed-height) ]

我只使用縱向。

我對它使用linearLayout。我如何計算listView高度以在屏幕頂部顯示微調,底部imageview和listview覆蓋所有空閒空間,但不會將其他人從視野中推開。

對於很多屏幕分辨率來說,它會很酷。

謝謝

回答

1

使用相對佈局來實現你所需要的。

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

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

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:src="@drawable/icon" /> 

    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/spinner1" 
     android:layout_above="@+id/imageView1" /> 

</RelativeLayout> 

通過這樣的列表視圖,使其微調和ImageView的屏幕

1

您需要使用android:layout_weight屬性的高度可變的項目,所以它填滿可用空間。

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

<Spinner 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 

<ListView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    /> 

<ImageView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 

</LinearLayout> 
+0

ListView是否仍然覆蓋這個例子中的圖片?它似乎是這樣。 – prolink007 2012-03-14 20:22:52

+0

試試吧,你會看到的。佈局使用layout_weight,它指定視圖之間的相對比率。 0表示固定大小,更大(例如,我使用的1)允許它伸展。因此,如果第一個和第三個視圖是固定尺寸,則在其高度上使用wrap_content,或設置明確的高度,例如, 30dp – 2012-03-15 09:13:35

+0

我只是檢查它沒有在佈局中的任何東西,它覆蓋,但我會嘗試添加的東西到佈局,看看。 – prolink007 2012-03-15 13:15:57

0

這裏就適合之間將調整其大小是另一種方式來做到這一點,利用weights。這可能是最簡單的方法。

您可以將weight放入您希望佔用的屏幕的百分比。

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

    <Spinner 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="10"/> 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="80"></ListView> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="10"/> 


</LinearLayout> 

編輯: 現在我想想,這將「修復」所有的部件的尺寸。他們將以屏幕百分比「固定」。這將與其他屏幕尺寸很好地擴展,但你確實說你想要ListView不被修復。