Android 列表视图中未显示顶部和底部分隔线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12139351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:51:35  来源:igfitidea点击:

Top and bottom dividers not showing in Android listview

androidlistviewdivider

提问by InterRaptor

The way I understand it, the divider defined for a listview should appear at the top and bottom of the list too, i.e. above the first item and under the last item.

按照我的理解,为列表视图定义的分隔符也应该出现在列表的顶部和底部,即第一项上方和最后一项下方。

For some reason they don't appear in my listview:

出于某种原因,它们没有出现在我的列表视图中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/background">

<ImageView
    android:id="@+id/home"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="3dp"
    android:paddingTop="3dp"
    android:paddingBottom="3dp"
    android:src="@drawable/homeicon"/>

<TextView
    android:id="@+id/titleBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Page Title" 
    android:layout_marginLeft="10dp" 
    android:paddingTop="3dp"
    android:textSize="18sp" 
    android:textStyle="bold" 
    android:typeface="serif" 
    android:textColor="#FFFFFF"
    android:layout_centerHorizontal="true"/>

<ImageView
    android:id="@+id/back"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingRight="8dp"
    android:paddingTop="8dp"
    android:src="@drawable/backicon" 
    android:layout_alignParentRight="true"/>


<ImageView
    android:id="@+id/separator1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/home"
    android:scaleType="fitXY"
    android:src="@drawable/separatorimg" />


<ListView
    android:id="@android:id/android:list" 
    android:layout_below="@+id/separator1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:cacheColorHint="#00000000"
    android:divider="@drawable/separatorimg">
</ListView>

</RelativeLayout>

Setting android:footerDividersEnabled to true (which is the answer given in other questions around that matter) doesn't change anything. Also I have no disabled items, unless there is somehow a default footer that would be disabled.

将 android:footerDividersEnabled 设置为 true (这是围绕该问题的其他问题中给出的答案)不会改变任何东西。我也没有禁用的项目,除非有某种默认页脚会被禁用。

Any ideas?

有任何想法吗?

Edit: It seems the divider at the top and bottom dividers appear for a split second when I scroll past the first/last item, and then disappears. It looks as if interface was going to continue, only later to realize that was the last item and then disable the divider.

编辑:当我滚动经过第一个/最后一个项目时,顶部和底部分隔线的分隔线似乎会出现一瞬间,然后消失。看起来好像界面要继续,后来才意识到这是最后一项,然后禁用分隔符。

回答by k3v

I solved this problem using help from the following link: Divider disappears from last item in listview if footerview added

我使用以下链接的帮助解决了这个问题:如果添加了footerview,分隔线将从列表视图中的最后一项消失

What I did was add the

我所做的是添加

  android:footerDividersEnabled="true" 

attribute to the ListView in XML. Then programatically, I added an empty footer view that was selectable using

属性到 XML 中的 ListView。然后以编程方式,我添加了一个空的页脚视图,可以使用

 listView.addFooterView(new View(context), null, true);

I didn't try the header, but I expect it to work the same way.

我没有尝试标题,但我希望它以相同的方式工作。

回答by Mugur

I had the same problem. Here's how I solved it:

我有同样的问题。这是我解决它的方法:

<ListView
    android:id="@+id/my_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="@color/transparent"
    android:dividerHeight="@dimen/padding_default"
    android:footerDividersEnabled="false"
    android:headerDividersEnabled="false" >
</ListView>

Then, in the onCreate() method, I have:

然后,在 onCreate() 方法中,我有:

View v = new View(this);
v.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 1));
mList.addFooterView(v);

回答by Archie.bpgc

I think you have to set footer using a view below the listview. i had the same problem and this is what i could get as a solution:

我认为您必须使用列表视图下方的视图设置页脚。我有同样的问题,这是我可以得到的解决方案:

Show divider after the last item in the list

在列表中的最后一项之后显示分隔线

回答by Eldhose M Babu

Please find the modified code below :

请在下面找到修改后的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background" >

<LinearLayout
    android:id="@+id/llAppBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:orientation="horizontal"
    android:weightSum="10" >

    <ImageView
        android:id="@+id/home"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:paddingBottom="3dp"
        android:paddingLeft="3dp"
        android:paddingTop="3dp"
        android:scaleType="fitXY"
        android:src="@drawable/homeicon" />

    <TextView
        android:id="@+id/titleBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_weight="6"
        android:paddingTop="3dp"
        android:text="Page Title"
        android:textColor="#FFFFFF"
        android:textSize="18sp"
        android:textStyle="bold"
        android:typeface="serif" />

    <ImageView
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:paddingRight="8dp"
        android:paddingTop="8dp"
        android:scaleType="fitXY"
        android:src="@drawable/backicon" />
</LinearLayout>

<ImageView
    android:id="@+id/separator1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/llAppBar"
    android:scaleType="fitXY"
    android:src="@drawable/separatorimg" />

<ListView
    android:id="@android:id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/separator1"
    android:cacheColorHint="#00000000"
    android:divider="@drawable/separatorimg" >
</ListView>

</RelativeLayout>

回答by tomoima525

I had the same problem, and setting padding and background color solved it.

我遇到了同样的问题,设置填充和背景颜色解决了它。

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/gray_background"
    android:paddingTop="1dp"
    android:paddingBottom="1dp"        
/> 

Here, background color should be same with that of divider with the listview.

在这里,背景颜色应该与列表视图中的分隔线的背景颜色相同。