【问题标题】:How to add a Footer View (TextView) to the end of a ListView?如何将页脚视图 (TextView) 添加到 ListView 的末尾?
【发布时间】:2014-03-13 18:53:25
【问题描述】:

我正在尝试制作这样的布局:

actionbar   
==================== 
txtViewTitle
--------------------
listview item1
listview item2
listview item3
........scroll scroll scroll
...at the end of all the items:
-----------------
txtViewFooter
==================
linearlayout of buttons [always visible at the bottom]
-------------------------------

==========s 之间的部分应该是可滚动的

这是我的代码,它的工作原理是 txtViewFooter 除外,如果列表足够短以容纳屏幕的一部分,则会显示该代码。但如果列表较长且需要滚动,则不显示

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/d_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
    android:id="@+id/txtViewTitle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="8dp"
    android:background="#eeeeee"
    android:gravity="center|center"
    android:layout_alignParentTop="true"
    style="?android:listSeparatorTextViewStyle"/>

<View
    android:layout_width="fill_parent"
    android:layout_height="0.1dp"
    android:layout_below="@+id/txtViewTitle"
    android:id="@+id/separator"
    android:visibility="visible"
    android:background="@android:color/darker_gray"/>

<ListView  
android:layout_height="match_parent"
android:layout_below="@+id/separator"
android:id="@+id/my_list" 
android:listSelector="@android:color/transparent"
android:smoothScrollbar="true"
android:layout_width="fill_parent"
android:background="@android:color/transparent"
    android:textColor="#000000"
    android:dividerHeight="0dp"
    android:cacheColorHint="@android:color/transparent">
</ListView>

<TextView
    android:id="@+id/txtViewFooter"
    android:layout_below="@+id/my_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp"
    android:textStyle="italic"
    />

<View
android:layout_width="fill_parent"
android:layout_height="0.1dp"
android:layout_above="@+id/bottom_menu"
android:id="@+id/separator2"
android:visibility="visible"
android:background="@android:color/darker_gray"/>

<LinearLayout android:id="@+id/bottom_menu"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"         
    android:orientation="horizontal" 
    android:layout_alignParentBottom="true"
    android:background="#eeeeee">
    <include layout="@layout/footer_menu" />
</LinearLayout>

【问题讨论】:

  • 你不能以你想要的方式使用页脚。列表视图中内置了页脚,就像一个额外的单元格,而不是末尾的括号。您只需添加一个单独的视图(如文本视图),然后以编程方式设置其内容,而不是使用页脚。

标签: android android-layout android-listview


【解决方案1】:

使用

ListView.addFooterView(View v)

为您的ListView 添加页脚。您不需要在 .xml 中定义页脚

例子:

TextView tv = new TextView(Context);
tv.setText("I am a footer")

ListView.addFooterView(tv);

从 .xml 膨胀:

View footer = LayoutInflater.from(Context).inflate(R.layout.your_footer_layout, null);
ListView.addFooterView(footer);

确保在将其他项目添加到列表之前添加页脚或页眉View。

【讨论】:

  • 在您的帖子中添加页眉和页脚对于 Android 的 UI 开发来说是一件很棒的事情,它可以在开发应用程序时产生巨大的影响,尤其是具有多个视图的应用程序。
  • 做到了!太感谢了! ^_^ 我不知道这个列表视图页脚的事情!
  • 等等。页脚视图的一部分隐藏在粘性底部菜单下方。如何解决这个问题?
  • 可能是另一个布局的问题,看看你的xml
猜你喜欢
  • 1970-01-01
  • 2015-03-13
  • 1970-01-01
  • 2021-06-05
  • 2017-01-28
  • 1970-01-01
  • 2011-04-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多