ListView Android中的页脚/页眉

时间:2013-07-06 22:13:43

标签: android listview

我无法使用listView获得页眉/页脚。我尝试了很多方法,但我不知道我哪里出错了。请在此处找到基本代码。

activity_main.xml中

<?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">
    <ListView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/listView"
            android:layout_gravity="center"/>
</LinearLayout>

header.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="5dip">

<Button android:id="@+id/previous"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />

</RelativeLayout>

MainActivity.java

package com.example.listview;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends Activity {

    private static final String[] lakes = {
            "Superior",
            "Victoria",
            "Huron",
            "Michigan",
            "Tanganyika",
            "Baikal",
            "Great Bear Lake",
            "Great Slave Lake",
            "Erie",
            "Winnipeg",
            "Ontario",
            "Balkhash",
            "Ladoga",
            "Vostok",
            "Onega",
            "Titicaca",
            "Nicaragua",
            "Athabasca" };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LayoutInflater inflater = this.getLayoutInflater();
        RelativeLayout listFooterView = (RelativeLayout)inflater.inflate(R.layout.header, null);

        ListView listView = (ListView)findViewById(R.id.listView);
        listView.addFooterView(listFooterView);

        ArrayAdapter<String> adapter =
                new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, lakes);
        listView.setAdapter(adapter);

    }
}

如果我删除listView.addFooterView(listFooterView); ,应用程序工作,并显示listView。但是,包含此语句会使应用程序崩溃。请让我知道这个问题的解决方案。

此致 Anubhav

0 个答案:

没有答案