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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2620177/
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 06:37:17  来源:igfitidea点击:

Android: Adding static header to the top of a ListActivity

androidheaderlistactivity

提问by crv

Currently I have a class that is extending the ListActivity class. I need to be able to add a few static buttons above the list that are always visible. I've attempted to grab the ListView using getListView() from within the class. Then I used addHeaderView(View) to add a small layout to the top of the screen.

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

Header.xml

头文件.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>

Before I set the adapter I do:

在我设置适配器之前,我会这样做:

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

This results in nothing happening to the ListView except for it being populated from my database. No buttons appear above it.

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

Another approach I tried as adding padding to the top of the ListView. When I did this it successfully moved down, however, if I added any above it, it pushed the ListView over. No matter what I do it seems as though I cannot put a few buttons above the ListView when I used the ListActivity.

我尝试的另一种方法是在 ListView 的顶部添加填充。当我这样做时,它成功地向下移动,但是,如果我在它上面添加任何东西,它会将 ListView 推过。无论我做什么,似乎在使用 ListActivity 时都无法在 ListView 上方放置几个按钮。

Thanks in advance.

提前致谢。

synic, I tried your suggestion previously. I tried it again just for the sake of sanity, and the button did not display. Below is the layout file for the activity and the code I've implemented in the oncreate().

synic,我之前试过你的建议。为了理智,我又试了一次,按钮没有显示。下面是活动的布局文件和我在 oncreate() 中实现的代码。

//My listactivity I am trying to add the header to

//我的列表活动我正在尝试将标题添加到

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 for activity:

活动的 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>

回答by synic

findViewById()only works to find subviews of the object View. It will not work on a layout id.

findViewById()仅适用于查找对象视图的子视图。它不适用于布局 ID。

You'll have to use layout inflater to convert the xml to it's corresponding View components. Something like this:

您必须使用 layout inflater 将 xml 转换为相应的 View 组件。像这样的东西:

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

I'm not sure why your code wasn't just throwing an error. findViewById()was probably just returning null, and so no header was added to your list.

我不确定为什么您的代码不只是抛出错误。 findViewById()可能刚刚返回null,因此没有将标题添加到您的列表中。

回答by pcu

Here is the simpliest sollution:

这是最简单的解决方案:

<?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>

or

或者

<?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>

instead of button you can add another horizontal linear layout

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

回答by crv

After some research I was able to figure out that mixing TableLayout and LinearLayout within my ListActivity XML document I was able to add a header to the document. Below is my XML document if anyone is interested to see it. While synic's approach is probably the right approach after work with his solution for sometime I was unable to make it function the way I wanted it.

经过一些研究,我发现在我的 ListActivity XML 文档中混合使用 TableLayout 和 LinearLayout 我能够向文档添加标题。下面是我的 XML 文档,如果有人有兴趣查看的话。虽然在使用他的解决方案一段时间后,synic 的方法可能是正确的方法,但我无法让它按照我想要的方式运行。

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

<?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>

回答by Melinda Green

The ListView answer above is useful but scrolls with the list and doesn't keep the header graphics at the top. The best solution I've found is to set a custom title to the activity. Here is what my constructor looks like:

上面的 ListView 答案很有用,但会随着列表滚动,并且不会将标题图形保留在顶部。我发现的最佳解决方案是为活动设置自定义标题。这是我的构造函数的样子:

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);
    ...

Where your_listview_layout.xml configures a ListView, and your_header.xml contains whatever custom header layout that you like. Just note that the three lines above must be called in exactly that order to not cause run-time problems.

your_listview_layout.xml 配置 ListView,而 your_header.xml 包含您喜欢的任何自定义标题布局。请注意,必须完全按照该顺序调用以上三行,以免导致运行时问题。

The tutorial that helped me was http://www.londatiga.net/it/how-to-create-custom-window-title-in-android/and you can find many related pages on Stack Overflow by searching for the term "setFeatureInt"

帮助我的教程是http://www.londatiga.net/it/how-to-create-custom-window-title-in-android/,您可以通过搜索术语“在 Stack Overflow 上找到许多相关页面” setFeatureInt"

回答by rsfrost

Adding a static header is easy, just make a separate relative view that has the alignParentTop (or bottom, right or left) attribute set to true.

添加静态标题很容易,只需创建一个单独的相对视图,将 alignParentTop(或底部、右侧或左侧)属性设置为 true。