Android:将静态标头添加到ListActivity的顶部

目前我有一个扩展ListActivity类的类。 我需要能够在列表上方添加一些始终可见的静态button。 我试图从类内使用getListView()获取ListView。 然后,我使用addHeaderView(View)在屏幕顶部添加一个小布局。

Header.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/testButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Income" android:textSize="15dip" android:layout_weight="1" /> </LinearLayout> 

在设置适配器之前,我需要:

 ListView lv = getListView(); lv.addHeaderView(findViewById(R.layout.header)); 

这导致没有任何事情发生ListView,除了它从我的数据库填充。 它上面没有button。

我尝试添加填充到ListView顶部的另一种方法。 当我做到这一点,它成功地移动,但是,如果我添加任何上面,它推动了ListView。 不pipe我做什么,似乎我不能在ListView上面放置几个button,当我使用ListActivity。

提前致谢。

synic,我以前试过你的build议。 为了理智,我再次尝试,并没有显示button。 下面是活动的布局文件和我在oncreate()中实现的代码。

//我的列表活动我试图添加标题

 public class AuditActivity extends ListActivity { Budget budget; @Override public void onCreate(Bundle savedInstanceState) { Cursor test; super.onCreate(savedInstanceState); setContentView(R.layout.audit); ListView lv = getListView(); LayoutInflater infalter = getLayoutInflater(); ViewGroup header = (ViewGroup) infalter.inflate(R.layout.header, lv, false); lv.addHeaderView(header); budget = new Budget(this); /* try { test = budget.getTransactions(); showEvents(test); } finally { } */ // switchTabSpecial(); } 

活动的Layout.xml:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@android:id/list" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@android:id/empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/empty" /> </LinearLayout> 

findViewById()只能用于查找对象View的子视图。 它不会在布局ID上工作。

您将不得不使用布局inflater将xml转换为相应的视图组件。 像这样的东西:

 ListView lv = getListView(); LayoutInflater inflater = getLayoutInflater(); View header = inflater.inflate(R.layout.header, lv, false); lv.addHeaderView(header, null, false); 

我不知道为什么你的代码不只是抛出一个错误。 findViewById()可能只是返回null ,所以没有头添加到您的列表。

这是最简单的溶剂:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/background"> <include layout="@layout/actionbar"/> <ListView android:id="@+id/tasklist_TaskListView" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:textColor="@color/baseFont"/> <include layout="@layout/bottombar"/> </LinearLayout> 

要么

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/background"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content"/> <ListView android:id="@+id/tasklist_TaskListView" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:textColor="@color/baseFont"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout> 

而不是button,您可以添加另一个水平线性布局

经过一番研究,我能够弄清楚在我的ListActivity XML文档中混合使用TableLayout和LinearLayout,我可以为文档添加一个标题。 下面是我的XML文档,如果有人有兴趣看到它。 虽然synic的方法可能是在他的解决scheme工作了一段时间后正确的方法,我无法使其function以我想要的方式。

AuditTab.java

 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.audittab); getListView().setEmptyView(findViewById(R.id.empty)); } 

audittab.xml

 <?xml version="1.0" encoding="utf-8"?> <TableLayout android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent"> <TableRow android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center_horizontal"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:layout_weight="1"> <Button android:id="@+id/btnFromDate" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" android:layout_weight="1" /> <Button android:id="@+id/btnToDate" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" android:layout_toRightOf="@+id/btnFromDate" android:layout_weight="1" /> <Button android:id="@+id/btnQuery" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Query" android:layout_toRightOf="@+id/btnToDate" android:layout_weight="1" /> </LinearLayout> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center_horizontal"> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@android:id/list" android:layout_width="300dip" android:layout_height="330dip" android:scrollbars="none" /> <TextView android:id="@+id/empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="10dip" android:text="- Please select a date range and press query." /> </LinearLayout> </TableRow> </TableLayout> 

AuditItem.xml

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:orientation="horizontal" android:padding="10sp"> <TextView android:id="@+id/transactionDateLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Date: " /> <TextView android:id="@+id/transactionDate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/transactionDateLabel" /> <TextView android:id="@+id/transactionTypeLabel" android:layout_below="@id/transactionDate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Type: " /> <TextView android:id="@+id/transactionType" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:layout_below="@id/transactionDate" android:layout_toRightOf="@id/transactionTypeLabel" /> <TextView android:id="@+id/transactionAmountLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:text="Amount: " android:layout_below="@id/transactionDate" android:layout_toRightOf="@id/transactionType" /> <TextView android:id="@+id/transactionAmount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/transactionDate" android:layout_toRightOf="@id/transactionAmountLabel" /> <TextView android:id="@+id/transactionCategoryLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Category: " android:layout_below="@id/transactionAmountLabel" /> <TextView android:id="@+id/transactionCategory" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/transactionAmountLabel" android:layout_toRightOf="@id/transactionCategoryLabel" /> <TextView android:id="@+id/transactionToAccountLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="To Account: " android:layout_below="@id/transactionCategoryLabel" /> <TextView android:id="@+id/transactionToAccount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/transactionCategoryLabel" android:layout_toRightOf="@id/transactionToAccountLabel" /> <TextView android:id="@+id/transactionFromAccountLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="From Account: " android:layout_below="@id/transactionToAccountLabel" /> <TextView android:id="@+id/transactionFromAccount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/transactionToAccountLabel" android:layout_toRightOf="@id/transactionFromAccountLabel" /> <TextView android:id="@+id/transactionNoteLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Note: " android:layout_below="@id/transactionFromAccountLabel" /> <TextView android:id="@+id/transactionNote" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/transactionFromAccountLabel" android:layout_toRightOf="@id/transactionNoteLabel" /> <Button android:id="@+id/editTransactionBtn" android:layout_width="wrap_content" android:layout_height="40sp" android:visibility="gone" android:text="Edit" android:layout_below="@id/transactionNoteLabel"/> <Button android:id="@+id/deleteTransactionBtn" android:layout_width="wrap_content" android:layout_height="40sp" android:text="Delete" android:layout_below="@+id/transactionNoteLabel" android:visibility="gone" android:layout_toRightOf="@+id/editTransactionBtn" android:ellipsize="end"/> </RelativeLayout> 

上面的ListView答案是有用的,但滚动列表,并不保留在顶部的标题graphics。 我find的最佳解决scheme是为活动设置自定义标题。 这是我的构造函数的样子:

 public void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.your_listview_layout); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.your_header); ... 

其中your_listview_layout.xmlconfigurationListView,而your_header.xml包含您喜欢的任何自定义页眉布局。 请注意,上面的三行必须按照正确的顺序调用,否则不会导致运行时问题。

帮助我的教程是http://www.londatiga.net/it/how-to-create-custom-window-title-in-android/ ,你可以find堆栈溢出许多相关页面通过search术语“ setFeatureInt”

添加一个静态头是很容易的,只需要将alignParentTop(或者bottom,right或left)属性设置为true的单独的相对视图。