【问题标题】:Android extra margin bottom automatically added to listview rowsAndroid 额外边距底部自动添加到列表视图行
【发布时间】: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


    【解决方案1】:

    是的,这是 ListView 的预期行为,这对于 recyclerview 不再适用。

    您必须按照此处指定的方式禁用分隔符:https://stackoverflow.com/a/1914588/802034

    getListView().setDivider(null);
    getListView().setDividerHeight(0);
    
    
    
    android:divider="@null"
    android:dividerHeight="0dp"
    

    【讨论】:

      【解决方案2】:

      首先,它不是导致问题的边距。它是列表视图中的分隔符。

      将其设置为 null as。

       <ListView
              android:id="@+id/lv"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:divider="@android:color/transparent"
              android:dividerHeight="0dp"
              android:background="#00FF00">
      </ListView>
      

      【讨论】:

        猜你喜欢
        • 2012-02-13
        • 2017-03-29
        • 2013-02-18
        • 1970-01-01
        • 2014-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多