【发布时间】:2017-03-26 00:56:40
【问题描述】:
我在 Android 中制作了一个带有一些任意元素的 ListView。 行布局是从 XML 文件扩展而来的。一切正常,但默认情况下每行都添加了一个 extra margin bottom,我从未在任何地方提到过。
我有意将 ListView 背景设置为绿色,行背景设置为蓝色,RelativeLayout 背景设置为红色。结果是每行都应用了 1/1.5 DP 边距底部。
这里的绿色边框实际上是 ListView 的背景色,表示两行之间有一些间隙。
这是 android ListView 中的预期行为,如果是,为什么它们会自动将边距底部添加到行? 以及如何消除/克服这种行为?
谢谢你,祝你有美好的一天
截图 -
MainActivity.java
package com.xxx.abcd;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] num = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve"};
ListView lv = (ListView) findViewById(R.id.lv);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, R.layout.row, num);
lv.setAdapter(arrayAdapter);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
tools:context="com.xxx.abcd.MainActivity">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00">
</ListView>
</RelativeLayout>
row.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:background="#214181"
android:textColor="#FFFFFF"
android:textSize="24sp"
android:padding="10dp">
</TextView>
【问题讨论】:
标签: android android-layout listview