【问题标题】:Android ListActivity - fixed header and footerAndroid ListActivity - 固定页眉和页脚
【发布时间】:2012-11-04 07:16:24
【问题描述】:
是否可以在 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);
【问题讨论】:
标签:
android
header
footer
listactivity
fixed
【解决方案1】:
您可以使用自定义 XML 布局来实现它,您将在其中设置页眉、页脚和列表的布局。
请注意,要与ListActivity 兼容,此布局必须包含一个ListView,其id 为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 中,如下所示:
public class TestActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
【解决方案2】:
根据项目情况,这种方法会比任何其他方法都好,因为如果您想自定义页眉和页脚,您可以在适当的页眉和页脚布局中进行更改。
在您的布局 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
}
}
【解决方案3】:
你可以这样做:
//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() 方法中。它对我有用。如果在这里发现任何错误,请给 cmets