2014-10-19 71 views
0

我有一個列表,我發現有太多的間距分開列表項目。我想減少它,但不知道如何。ListView:刪除項目之間的空間,並限制列表視圖中的項目數

下面是佈局文件:

<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" 
       android:background="#f2f2f2" 
       > 

    <ListView 
     android:id="@+id/listMessages" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/divider" 
     android:layout_below="@+id/iSchedule" 
     android:divider="@null" 
     android:dividerHeight="0dp" 
     android:drawSelectorOnTop="false" 
     android:padding="0dip" 


     android:stackFromBottom="true" 
     android:transcriptMode="alwaysScroll" 
     tools:listitem="@layout/message_left" /> 

    <RelativeLayout 
     android:id="@+id/divider" 
     android:layout_width="fill_parent" 
     android:layout_height="1dip" 
     android:background="@color/off_white" 
     android:layout_above="@+id/relSendMessage" /> 

    <RelativeLayout 
      android:id="@+id/relSendMessage" 
      android:layout_width="wrap_content" 
      android:layout_height="48dp" 
      android:background="#ddd" 
      android:paddingLeft="10dp" 
      android:layout_alignParentBottom="true"> 

     <EditText 
      android:id="@+id/messageBodyField" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_alignBottom="@+id/sendButton" 
      android:layout_alignTop="@+id/sendButton" 
      android:layout_marginBottom="-4dp" 
      android:layout_marginRight="10dp" 
      android:layout_toLeftOf="@+id/sendButton" 
      android:background="@android:color/white" 
      android:hint="@string/message_elipses" 
      android:inputType="text|textNoSuggestions|none" 
      android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ " 

      android:textColor="#000000" 
      android:textColorLink="#adefda" 
      android:textSize="14sp" /> 

     <Button 
       android:id="@+id/sendButton" 
       android:layout_width="72dp" 
       android:layout_height="match_parent" 
       android:layout_alignParentRight="true" 
       android:layout_margin="4dp" 
       android:background="@drawable/button_send" /> 
    </RelativeLayout> 

    <Button 
     android:id="@+id/iSchedule" 
     android:layout_width="105dp" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:alpha="0.7" 
     android:background="#C11B17" 
     android:text="event" 
     android:textColor="#f2f2f2" 
     android:textSize="20sp" 
     android:textStyle="bold" 
     android:typeface="serif" /> 

    <com.parse.ParseImageView 
     android:id="@+id/iProfilempic1" 
     android:layout_width="50dp" 
     android:layout_height="100dp" 
     android:layout_alignBottom="@+id/iSchedule" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:src="@drawable/abc_ab_solid_light_holo" /> 

    <TextView 
     android:id="@+id/tMName1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/tMActivityName1" 
     android:textColor="#292826" 
     android:textSize="16sp" 
     android:layout_marginLeft="2dp" 
     android:maxLength="40" 
     android:textStyle="bold" 
     android:typeface="serif" /> 

    <TextView 
     android:id="@+id/tMActivityName1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
       android:layout_marginLeft="2dp" 

     android:layout_below="@+id/tMName1" 
     android:layout_toRightOf="@+id/iProfilempic1" 
       android:maxLength="40" 

     android:textColor="#292826" 
     android:textSize="16sp" 
     android:typeface="serif" /> 

</RelativeLayout> 

我也想限制列表中的25,其中不超過25個項目,可以顯示在列表中的項目數量,如果我想知道可以在佈局內實現這一點,而不是可編程的(不使用自定義適配器)。

下面是如何列表聲明在java代碼:

messagesList = (ListView) findViewById(R.id.listMessages); 
     messageAdapter = new MessageAdapter(this); 
     messagesList.setAdapter(messageAdapter); 
+0

您的列表項目佈局是什麼樣的? – mjp66 2014-10-19 11:13:33

回答

1

你已經有dividerHeight=0dp。這要儘可能接近。唯一可能解釋項目之間的額外間距的其他事情是實際項目視圖中的填充。你可能想減少填充。

至於你的限制25,這是不可能的,你指定的方式。限制這種情況的唯一方法是以編程方式限制適配器的內容。實現此目的的一種非常簡單的方法(使用ArrayAdapter):

private void addItems(Collection<String> items) { 
    for (String item : items) { 
     if (mAdapter.getCount() < 25) { 
      mAdapter.add(item); 
     } 
    } 
} 
+0

感謝您的建議。我已經解決了第一個案子。對於第二個,是否可以在不使用自定義適配器的情況下設置限制?如果是這樣,你會在這個旅程中指導我嗎?非常感謝 – John 2014-10-19 11:20:21