【问题标题】:listview extra spacing between itemslistview 项目之间的额外间距
【发布时间】:2013-11-22 03:05:01
【问题描述】:

我有一个图像列表视图,我似乎无法从垂直项目之间获得额外的间距。尝试了很多东西,但到目前为止没有运气。我似乎无法弄清楚间距是从哪里来的。

这是包含 ListView 的 XML(这是一个自定义列表视图,可以进行拖动/排序)

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:id="@+id/fan_ban_ctn"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.1"
    android:background="@color/white"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/fan_button"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_gravity="left|center_vertical"
        android:layout_weight="0.33"
        android:text="@string/fan"
        android:textSize="14sp" />

    <Button
        android:id="@+id/ban_button"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_gravity="center_vertical|center_horizontal"
        android:layout_weight="0.33"
        android:text="@string/ban"
        android:textSize="14sp" />

    <Button
        android:id="@+id/ctn_button"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_gravity="right|center_vertical"
        android:layout_weight="0.33"
        android:text="@string/ctn"
        android:textSize="14sp" />
</LinearLayout>

<LinearLayout
    android:id="@+id/wireless_mgmt"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.9"
    android:background="@color/white"
    android:orientation="horizontal"
    android:weightSum="1.0" >

    <com.att.attbusiness.widget.DynamicListView
        android:id="@+id/widget_list"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.10"
        android:divider="@android:color/transparent"
        android:dividerHeight="2dp"/>

    <ScrollView
        android:id="@+id/widget_content"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_marginRight="12dp"
        android:layout_weight="0.85"
        android:background="@color/white" >

        <FrameLayout
            android:id="@+id/wireless_fragment_Container"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </ScrollView>
</LinearLayout>

列表项在这里:

<ImageView
    android:id="@+id/widget_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/wm_plan" />

当我以编程方式在自定义 Adaper 中添加额外的 ImageViews 时,我得到了这个 结果:

如何减少图像之间的间距?

虽然我以编程方式将图像添加到列表视图,但我不做任何布局或边距或任何事情 - 只是图像。然而,我确实以不同的方式阅读我的图像。我从使用一些字符串操作构建的资源 id 加载并将位图分配给 ArrayLists。我有一个选定的和未选定的版本,两者的大小相同。

private final static String[] widgetNames = new String[] {
    "wm_device", "wm_userinfo", "wm_billing", "wm_plan", "wm_features", "wm_access_options", "wm_applications", 
    "wm_analytics", "wm_manage_a_list"
};

<...>

for (int i = 0; i < widgetNames.length; ++i) {
        int normalID = this.getResources().getIdentifier(widgetNamesOrdered[i], "drawable", "com.att.attbusiness");
        int selectID = this.getResources().getIdentifier(widgetNamesOrdered[i]+"_selected", "drawable", "com.att.attbusiness");
        mWidgetList.add(widgetNamesOrdered[i]);         

        if (normalID != 0) {
            widgetIcons.add(i, (Bitmap) BitmapFactory.decodeResource(this.getResources(), normalID));
        }           

        if (selectID != 0) {
            widgetIconsSelected.add(i, (Bitmap) BitmapFactory.decodeResource(this.getResources(), selectID));
        }


    }

这是我的自定义适配器的一部分,它在列表视图中设置图像:

// if this button isn't the selected, make sure it's off
        if(mSelectedPosition != position){

            holder.widget.setImageBitmap(widgetIcons.get(position));
        }
        else {
            holder.widget.setImageBitmap(widgetIconsSelected.get(position));
            if(mSelectedBtn != null && holder.widget != mSelectedBtn){
                mSelectedBtn = holder.widget;
            }
        }

【问题讨论】:

  • 显示创建 ImageView 并以编程方式将其添加到您的代码中。
  • 在我的自定义适配器中添加了我的 getView() 部分
  • 您是否尝试过将您的 LinearLayouts 设置为 layout_height="wrap_content"?,尤其是包含 ListView 的那个

标签: android


【解决方案1】:

您是否在 xml 文件中尝试过 android:layout_marginTop="0dp" ?否则我只会使用列表视图而不是线性布局

【讨论】:

  • 将顶部和底部的边距设置为负值,但当我只是将图像放入列表时,这样做似乎并不正确。
  • 我明白,但是由于您实际上是在 java 文件中而不是在 xml 中构建列表(您有线性布局),所以这是最好的解决方案。
  • 另一种可能性是您希望列表重叠,因为其中的内容不会占据整个部分
  • 不,不需要或不需要重叠。该列表受其元素向上或向下拖动的影响,因此它们需要完全可见。该列表占用的空间超过了屏幕大小,因此占用了整个空间