删除空格ListView Android

时间:2016-06-02 07:57:15

标签: java android listview android-studio

我正在使用自定义列表视图,每行使用2张图片视图作为背景。一切都很好,除了每个项目之间留有空白的事实。这是我的代码:

首先是我的list_row.xml。请注意,2个图像视图是图像和暗覆盖,使第一个图像更暗,文本更容易阅读。它们充当每一行的背景。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<!-- BG Image -->
<ImageView
    android:id="@+id/image_bg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"/>

<!-- Dark Overlay for BG Image -->
<ImageView
    android:id="@+id/image_bg_over"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true" />

<TextView
    android:id="@+id/filler"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:layout_centerHorizontal="true" />

<!-- Bar Name -->
<TextView
    android:id="@+id/barname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_below="@id/filler"
    android:textSize="@dimen/title"
    android:textStyle="bold"
    android:textColor="@color/list_text_color"/>

<!-- Address -->
<TextView
    android:id="@+id/address"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/barname"
    android:gravity="center"
    android:layout_marginTop="1dip"
    android:textColor="@color/list_text_color"
    android:textSize="@dimen/rating"/>

<!-- Text CheckIn / CheckOut -->
<TextView
    android:id="@+id/check"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/address"
    android:layout_marginTop="10dp"
    android:layout_centerHorizontal="true"
    android:textColor="@color/list_text_color"
    android:textSize="@dimen/genre"/>

<!-- Image CheckIn / CheckOut -->
<ImageView
    android:id="@+id/checkImg"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_marginTop="10dp"
    android:layout_below="@+id/address"
    android:gravity="right"
    android:layout_toRightOf="@+id/check"
    android:adjustViewBounds="true" />


</RelativeLayout>

然后我的适配器:

public View getView(int position, View convertView, ViewGroup parent) {
    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.list_row, null);

    ImageView bgImg = (ImageView) convertView.findViewById(R.id.image_bg);
    ImageView bgImgOver = (ImageView) convertView.findViewById(R.id.image_bg_over);

    TextView barname = (TextView) convertView.findViewById(R.id.barname);
    TextView address = (TextView) convertView.findViewById(R.id.address);
    ImageView checkImg = (ImageView) convertView.findViewById(R.id.checkImg);
    TextView check = (TextView) convertView.findViewById(R.id.check);

    Bar m = barItems.get(position);

    bgImg.setImageResource(R.drawable.bg_default);
    bgImgOver.setImageResource(R.drawable.bg_darkover);

    barname.setText(m.getPlaceName());
    address.setText(m.getAddress());
    check.setText("Check-In");
    checkImg.setImageResource(R.drawable.ic_checkin);

    return convertView;
}

片段的xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:id="@+id/places_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</RelativeLayout>

我创建listview的片段的onViewCreated方法:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) { //Otherwise can't use findViewById
    listView = (ListView) getView().findViewById(R.id.list);
    adapter = new CustomListAdapter(getActivity(), barList);
    listView.setAdapter(adapter);

    Bar bar1 = new Bar();
    bar1.setPlaceName("place1");
    bar1.setAddress("Addr1");
    barList.add(bar1);
    Bar bar2 = new Bar();
    bar2.setPlaceName("place2");
    bar2.setAddress("Addr2");
    barList.add(bar2);
    Bar bar3 = new Bar();
    bar3.setPlaceName("place3");
    bar3.setAddress("Addr3");
    barList.add(bar3);
    adapter.notifyDataSetChanged();

    listView.setDivider(null);
    listView.setDividerHeight(0);
}

使用setDivider我可以删除空白区域内的黑色分隔符,但不能删除空白区域。您可以在屏幕截图中看到空格 listView_blank_space

我认为我在相对布局中犯了一个错误,创造了这个空间,但我找不到任何帮助,我不知道!

2 个答案:

答案 0 :(得分:1)

<!-- BG Image -->
<ImageView
    android:id="@+id/image_bg"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitXY"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"/>

<!-- Dark Overlay for BG Image -->

的相同选项

并为顶部项RelativeLayout设置固定高度;

答案 1 :(得分:0)

我认为你的图像背景不够高,将这2个属性添加到图像背景中并查看

 android:scaleType="centerCrop"