【问题标题】:Android ListView addFooterView margin/padding issueAndroid ListView addFooterView 边距/填充问题
【发布时间】:2013-12-12 09:52:19
【问题描述】:

我有一个 ListView,它有一个 LinearLayout 作为它的页脚视图,其中包含一个 TextView 和一个 ListView,如下所示:

mylist = (ListView) findViewById(R.id.mylist);
mylistadapter = new MyListAdapter(MainView.this, info);
myfooterlistadapter = new MyListAdapter(MainView.this, info);
LinearLayout footer = (LinearLayout)getLayoutInflater().inflate(R.layout.myfooter, null);
myfooterlist = (ListView) footer.findViewById(R.id.myfooterlist);
mylist.addFooterView(footer, null, false);

mylist 声明为:

<ListView
    android:id="@+id/mylist"
    android:layout_width="fill_parent"
    android:layout_height="0dip" <!-- have also tried fill_parent without weight, same-->
    android:layout_weight="1"
    android:layout_marginLeft="5dip"
    android:layout_marginRight="5dip"
    android:cacheColorHint="#00000000"
    android:dividerHeight="1dip"
    android:drawSelectorOnTop="false" />

页脚(LinearLayout声明为):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/myfooter" >

    <TextView
        android:id="@+id/myfooterlabel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/headermainview"
        android:paddingLeft="2dip"
        android:text="@string/myfootertext"
        android:textColor="@color/black"
        android:textSize="16sp"
        android:textStyle="bold"
        android:visibility="gone" />

    <ListView
        android:id="@+id/myfooterlist"
        android:layout_width="fill_parent"
        android:layout_height="0dip" <!-- have also tried fill_parent without weight, same-->
        android:layout_weight="1"
        android:cacheColorHint="#00000000"
        android:dividerHeight="1dip"
        android:drawSelectorOnTop="false"
        android:visibility="gone"
        android:background="@color/green" />

</LinearLayout>

如果我的页脚列表有项目,我将 TextView 和页脚 ListView 的可见性更改为可见。

现在,我在页脚列表视图上将背景涂成绿色以查看问题(抱歉隐藏了文本,但这是机密信息)。如您所见,我在页脚列表的底部不断获得一些额外的边距。我已经尝试将列表和 LinearLayout 从 fill_parent 更改为 wrap_content 到 0dip,其中 weight=1 但没有任何效果。如您所见,我没有任何与在页脚列表的最后一项之后添加额外边距相关的内容。 顶部列表有 20 项(我已滚动到屏幕底部),而我的页脚列表仅在这种情况下有 1 项:

任何想法发生了什么以及如何在我的页脚列表底部没有这个额外的边距?谢谢!

更新:

经过进一步检查,似乎无论第二个列表的大小(我添加了更多要查看的项目),高度都保持不变(绿色框始终大小相同)。我在 LinearLayout (无重量)和第二个列表上更改为 fill_parent 高度,但大小仍然相同。 ListView 不是动态计算footer 的高度才知道做多大的吗?

我正在添加一个新的屏幕截图,以更详细地向您展示第二个项目被切断(顶部列表没有延伸到覆盖整个第二个列表)。

更新 2:

经过进一步调查,如果我将第二个 ListView (myfooterlist) 的高度设置为特定高度(比如 300dip),那么当我滚动到底部时,第一个列表会显示整个第二个列表。如果我将它设置为 fill_parent 或 wrap_conent 或 0dp 并添加 layout_weight=1 那么它给我的高度与上面的图片相同。知道动态计算高度是怎么回事吗?

【问题讨论】:

  • 是否有任何特定原因将第二个列表视图显示为页脚?它可以对齐第一个列表视图的底部..对吗?
  • 将页脚部分保留在单独的布局中。还要从页脚布局中删除第二个列表视图。
  • 是的,我想滚动到第一个列表的底部,然后显示 TextView 和页脚列表。 LinearLayout 位于另一个名为 myfooter.xml 的文件中。

标签: android listview margin footer


【解决方案1】:

我认为您不能在 ListView 的页脚中嵌入第二个列表,我认为这是您问题的根源。在同一个视图(或屏幕)中嵌入多个可滚动视图时,通常会出现问题。

将第二个列表视图移出您的页脚,我认为它会正确显示 - 我认为您不能将它嵌入到您的页脚中。

【讨论】:

  • 请参阅更新 2。毫无疑问,这将起作用,但问题是我必须修复第二个列表以采用特定大小的初始屏幕(滚动到它的功能不起作用因为我必须将它包装在一个不好的 ScrollView 中)。
  • 这在 SO 中得到了特别的解决。问题是您不能在单个滚动视图中拥有多个可滚动项目(例如您的多个列表)。 (stackoverflow.com/questions/16515879/…)。
  • 出于所有意图和目的,我不希望滚动第二个列表,因为它没有意义,因为第一个列表将滚动浏览所有内容。所以从这个意义上说,它不像其他需要在滚动视图内滚动列表视图的问题。我试图根据它的子项动态计算列表的高度,但它不是 100% 准确,因为并非所有项目都具有相同的高度。