Android:将静态标题添加到 ListActivity 的顶部 [英] Android: Adding static header to the top of a ListActivity

查看:21
本文介绍了Android:将静态标题添加到 ListActivity 的顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

我尝试的另一种方法是将填充添加到 ListView 的顶部.当我这样做时,它成功地向下移动,但是,如果我在它上面添加任何内容,它会将 ListView 推过来.无论我做什么,当我使用 ListActivity 时,似乎都无法在 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.

提前致谢.

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

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

//我的listactivity我正在尝试将标题添加到

//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:

Layout.xml for activity:

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

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

您必须使用布局充气器将 xml 转换为相应的视图组件.像这样的:

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

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

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

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.

这篇关于Android:将静态标题添加到 ListActivity 的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆