2013-05-08 47 views
1

我有列表視圖,我想填充整個屏幕,有四個項目在列表視圖。四個項目填滿後,它會留下空的空間。您可以在屏幕截圖中看到。它留下了空白空間。我希望整個屏幕被覆蓋。使列表視圖佔據整個屏幕

enter image description here

我想有這樣的:

enter image description here

這裏是源代碼

MainActivity.java。

public class MainActivity extends Activity { 
    ListView resultPane; 
    List<Taskinfo> list; 
    CustomAdapter adapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     resultPane = (ListView) findViewById(R.id.mylist); 
     list = new ArrayList<Taskinfo>(); 
     Resources res = getResources(); // resource handle 
     Drawable drawable = res.getDrawable(R.drawable.browse_home); 

     list.add(new Taskinfo("Browse", drawable)); 
     drawable = res.getDrawable(R.drawable.jewelry); 
     list.add(new Taskinfo("Whats New", drawable)); 
     drawable = res.getDrawable(R.drawable.show); 
     list.add(new Taskinfo("Upcoming Show", drawable)); 
     drawable = res.getDrawable(R.drawable.contact); 
     list.add(new Taskinfo("Contact Us", drawable)); 

     adapter = new CustomAdapter(this, list); 
     resultPane.setAdapter(adapter); 



    } 

} 

CustomAdapter.java

public class CustomAdapter extends BaseAdapter { 
    private Context context; 
    private List<Taskinfo> list; 

    public CustomAdapter(Context context, List<Taskinfo> list) { 
     this.context = context; 
     this.list = list; 

    } 

    public View getView(int index, View view, final ViewGroup parent) { 

     if (view == null) { 
      LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 
      view = inflater.inflate(R.layout.single_list_item, parent, false); 
     } 

     Taskinfo t = list.get(index); 

     RelativeLayout l = (RelativeLayout) view 
       .findViewById(R.id.testrelative); 

     l.setBackgroundDrawable(t.getImage()); 

     TextView textView = (TextView) view.findViewById(R.id.title); 
     textView.setText(t.getName()); 

     return view; 
    } 

} 

single_list_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/testrelative" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/browse_home" 
    android:orientation="horizontal" 
    android:padding="5dip" > 

    <!-- ListRow Left sied Thumbnail image --> 

    <LinearLayout 
     android:id="@+id/thumbnail" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_marginRight="5dip" 

     android:padding="3dip" > 
    </LinearLayout> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="16dp" 
     android:text="" 
     android:textColor="#040404" 
     android:textSize="15dip" 
     android:textStyle="bold" 
     android:typeface="sans" /> 

</RelativeLayout> 

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="45sp" 
     android:id="@+id/llayout" 
     android:background="@drawable/navbar" 
     android:padding="3sp" > 



     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical|center_horizontal" 
      android:layout_weight="1" 
      android:gravity="center_horizontal" 
      android:text="Golden Stone" 

      android:textColor="@color/white" 
      android:textSize="20sp" > 
     </TextView> 


    </LinearLayout> 


    <ListView 
     android:id="@+id/mylist" 
     android:layout_width="match_parent" 
     android:layout_below="@id/llayout" 
     android:layout_height="match_parent" > 
    </ListView> 

</RelativeLayout> 
+0

據我所知,你需要設置列表中視圖的高度來適應屏幕,如果是的話,這將通過代碼 – 2013-05-08 19:43:06

+0

完成,使用linearlayout並讓它適合整個屏幕 – 2013-05-08 19:43:34

+0

謝謝@MoshErsan有效。我使用代碼根據屏幕大小動態分配高度。 – 2013-05-08 21:05:08

回答

1

你會想設置ListView的背景。目前,您的視圖應該到底,但ListView的背景是透明的,因此將其設置爲白色應該可以實現您所要求的。

<ListView ... 
    android:background="@android:color/white" 
... /> 
+0

不添加背景,因爲白色將不起作用。它只是開始新的項目。你可以在這裏查看http://img28.imageshack.us/img28/1822/device20130509011734.png – 2013-05-08 19:49:40

+0

它不會啓動一個新的列表項 - 但它確實可以防止列表在它應該出現之前結束。但是因爲您似乎希望將4個項目均勻分佈,請採用fdreger的建議並使用帶有權重的LinearLayout。 – 2013-05-08 19:53:25

2

在這種情況下使用ListView是奇怪的和不必要的。考慮一下你的約束條件(比如:如果項目溢出?),並使用適當的佈局管理器。與垂直LinearLayout一樣,最後一項的佈局權重不爲零。

ListView奇妝感只有當你需要一臺即時生成列表項的抽象。

+0

我現在有這個問題。客戶想要這樣,所以我必須這樣實施。你會進一步描述你的建議嗎? – 2013-05-08 19:51:32

+0

@TonyChhabada:你沒有道理。你的問題源於你正在嘗試做一些'LinearLayout'完美的事情(可能包裝到ScrollView中),但出於某種奇怪的原因,堅持要做一些完全相同的事情,但需要一些瘋狂的'ListView'組合。 – fdreger 2013-05-08 20:05:25

+0

@TonyChhabada:忽略所有適配器,列表視圖和內容。只要做一個線性佈局,四次膨脹你的single_row,放入適當的drawable並添加到佈局中。當你添加最後一個時,設置一些佈局權重。完成。 – fdreger 2013-05-08 20:14:26

0

如果有這麼幾個項目,你可以使用的LinearLayout代替,並給予的權重爲它的每個項目。

然而,這樣做請注意,Android支持多種設備和屏幕上,所以你可能想擁有的每一行會多小的限制。

無論如何,如果你想使每一行的安裝高度,你可以檢查其大小,然後通過項目的數量劃分。

,以獲得列表視圖的大小,你可以使用this small snippet我所做的。