2012-06-15 129 views
0

我已經在我的Android應用程序(API 8)中實現了一個listview。但是,該列表僅包含三個元素,因此,超過一半的屏幕是空的。我想擴展列表視圖以覆蓋手機的整個屏幕。有什麼辦法可以做到這一點?ListView覆蓋Android中的整個屏幕

這裏是我的佈局XML文件:

<?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" 
android:background="@color/white" 
android:shape="rectangle" 
android:paddingTop="5dp" 
android:paddingBottom="0dp" 
> 


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

    <ImageView 
     android:layout_weight="40" 
     android:id="@+id/test_image" 
     android:layout_width="158dp" 
     android:layout_height="52dp" 
     android:contentDescription="@string/footer" 
     android:src="@drawable/logo1" /> 

    <Spinner 
     android:id="@+id/spinner1" 
     android:layout_width="127dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:layout_weight="60" 
     android:prompt="@string/city_prompt" 
     /> 
</LinearLayout> 

    <LinearLayout android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    > 
    <ListView 
    android:id="@android:id/list" 
    android:layout_weight="1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#000000" 
    android:drawSelectorOnTop="true" 
    android:layout_gravity="center" 
    android:dividerHeight="2dp" 
    /> 


</LinearLayout> 


</LinearLayout> 

請注意,我想要的清單項目,以覆蓋整個屏幕,在地方只有三個項目。

+0

刪除LinearLayout的ListView的父級並嘗試。此外,我也有同樣的問題,listView沒有填滿整個屏幕。 – Sana

+0

看到我的問題http://stackoverflow.com/questions/10943410/listview-doesnt-occupy-whole-screen – Sana

+0

你可以嘗試使用android:dividerHeight嗎? –

回答

1

第一個變化:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="@color/white" 
    android:shape="rectangle" 
    android:paddingTop="5dp" 
    android:paddingBottom="0dp" 
> 

Tthen第二,變化:

<ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#000000" 
    android:drawSelectorOnTop="true" 
    android:layout_gravity="center" 
    android:dividerHeight="2dp" 
/> 
+0

不好意思讓你失望,但即使這樣也行不通! – rahulritesh

+0

這是因爲fill_parent = match_parent。 http://stackoverflow.com/questions/5761960/what-is-the-difference-between-match-parent-and-fill-parent-property-in-android –

+0

@rahulritesh使用佈局inflater使你自己的行和設置該行的高度。 –

2

的ListView不會自動補全的空間。如果你想實現這一點,因爲在這裏,ListView佔據了整個空間,但不是它的項目。如果您仍然希望製作更大的行,那麼在較小的項目的情況下,請使用getCount()方法在getView方法中提供列表項的高度。

例如,要在列表中的屏幕上一次顯示最多六個項目。然後實現ListView的是這樣的:

獲取寬度和屏幕的高度,使用下面的代碼到onCreate方法:在getView

Display display = getWindowManager().getDefaultDisplay(); 
Point size = new Point(); 
display.getSize(size); 
int width = size.x; 
int height = size.y; 

現在,請執行下列操作:

int itemHeight = 0; 
if (getCount() >= 6) 
    itemHeight = height/6; 
else 
    itemHeight = height/getCount(); 

LinearLayout llView=new LinearLayout(YourActivityName.this); 
//Or inflate through XML 

llView.setLayoutParams(new LayoutParams(LayoutParams.Fill_Parent, itemHeight)); 
//Do other functionality 
+0

你能詳細解釋一下嗎? – rahulritesh

+0

plz檢查我編輯的答案。 – jeet

+0

嘿!謝謝你:) 我粘貼了onCreate方法中的第一個代碼。但是,它在display.getSize(size);上顯示錯誤。 (錯誤:方法getSize(Point)未定義類型顯示) 另外,你能告訴我如何把第二個代碼。我目前沒有getView功能! – rahulritesh

2

我只是複製你的XML,粘貼並嘗試,並且它工作正常。還有一件事要說的是,你不應該使用layout_height="wrap_content"。相反,您可以將其作爲android:layout_height="0dip",因爲您已經使用android:layout_weight="1"這將使ListView填充您的LinearLayout的其餘區域。

+0

請參閱編輯! – rahulritesh

+1

如果行數少,您應該找到一種方法來增加列表視圖的行高,否則不會有效。 –