java - ListView:删除项目之间的空格,并限制 ListView 中的项目数量

标签 java android android-layout listview android-activity

我有一个列表,我发现列表项之间的间距太大。我想减少它,但不知道如何。

下面是布局文件:

<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);

最佳答案

您已经有 dividerHeight=0dp。这已经是最接近的了。唯一可以解释项目之间额外间距的另一件事是实际项目 View 本身的填充。您可能想减少填充。

至于您的限制为 25,按照您指定的方式这是不可能的。限制这一点的唯一方法是以编程方式限制适配器的内容。实现此目的的一个非常简单的方法(使用 ArrayAdapter ):

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

关于java - ListView:删除项目之间的空格,并限制 ListView 中的项目数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26449867/

相关文章:

javascript - Java servlet 代码没有被调用?

java - 一个老问题,但我还没有解决方案 : java. sql.SQLException: Access denied for user 'root' @'localhost' (using password: NO)

android - 在 RelativeLayout 中设置 minHeight,禁用 alignBaseline?

java - 我对如何禁用更改为单击另一个单选按钮感兴趣?

java - 套接字编程的实现和接口(interface)

android - 在 RecyclerView 下方放置一个 View

java - Cassandra - 有没有办法限制异步查询的数量?

java - 在不使用任何第三方的情况下将 map 数据映射到同一卡片 View 中的 Recyclerview Adapter

java - Android 应用程序在尝试打开 SettingsFragment 后崩溃

android - 从 SQLite 数据库中删除需要很长时间,导致挂起