Android ListActivity - 固定页眉和页脚

时间:2012-11-15 16:21:37

标签: android header footer listactivity fixed

是否可以将ListActivity的页眉和页脚设置为固定在顶部和底部,因此只有内容(列表)滚动,而不是页眉和页脚?

我都这样设置:

View header = getLayoutInflater().inflate(R.layout.list_header, null);
View footer = getLayoutInflater().inflate(R.layout.list_footer, null);
ListView listView = getListView();
listView.addHeaderView(header);
listView.addFooterView(footer);

3 个答案:

答案 0 :(得分:11)

您可以使用自定义XML布局来设置页眉,页脚和列表的布局。

请注意,要与ListActivity兼容,此布局必须包含ID为ListView的{​​{1}}:

android.R.id.list

并将其设置在<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="HEADER" /> <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="FOOTER" /> </LinearLayout> 中,如下所示:

ListActivity

答案 1 :(得分:3)

根据项目场景,此方法优于任何其他方法,因为如果要自定义页眉和页脚,可以在适当的页眉和页脚布局中进行更改。 在你的布局xml文件中,请按照:

在main.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" >

   <include
        android:id="@+id/header_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        layout="@layout/header.xml" />

   <ListView
        android:id="@+id/list_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:below="@id/header_layout"
        android:above="@id/footer_layout" />

    <include
        android:id="@+id/footer_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        layout="@layout/footer.xml" />
 </RelativeLayout>

在header.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="vertical" >

    <TextView
           android:id="@+id/header_text_view"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" 
           android:text="Your Application Title"/>
   </LinearLayout>

在footer.xml中

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="vertical" >

     <Button
           android:id="@+id/done_button"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" 
           android:text="Done"/>
   </LinearLayout>

在活动中,这里

  public class MainActivity extends ListActivity {

   private ListView mListView;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mListView = (ListView) findViewById(R.id.list_view);
        // Now you can do whatever you want

   }
 }

答案 2 :(得分:0)

你可以这样做,如下所示:

    //your adapter for listview
    mAdapter = new ToDoListAdapter(getApplicationContext());

    // Put divider between ToDoItems and FooterView
    getListView().setFooterDividersEnabled(true);

    // TODO - Inflate footerView for footer_view.xml file
    LayoutInflater layoutInflater = getLayoutInflater();

    //text view or whatever you have for your footer
    TextView footerView = null;
    footerView=(TextView)layoutInflater.inflate(R.layout.footer_view,null);

    // TODO - Add footerView to ListView

    getListView().addFooterView(footerView);
    getListView().setAdapter(mAdapter);

这是在你的onCreate()方法中。它对我有用。如果你发现任何错误,请发表评论