In an Android ListActivity, I am adding a button as a footer in a ListView. How can I apply layout formatting to this button like centering it and/or width=fill_parent?
I have tried a few things such as using setLayoutParams(), but have not gotten it to work - the footer always disappears when I try anything.
Here is the basic code I am working with
closeButton = new Button(this);
closeButton.setText(getResources().getString(R.string.title_closeprocess));
closeButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
closeProcess();
}
});
getListView().addFooterView(closeButton);
*update
Based on Christopher's answer, here is the footer view that I am adding to my listview, and the activity code that 开发者_开发技巧adds it to the listview:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/closebutton_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toptable"
android:layout_width="fill_parent"
android:stretchColumns="*"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content">
<TableRow>
<Button android:id="@+id/closebutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Skip Question"
android:gravity="center"
android:paddingTop="16dip">
</Button>
</TableRow>
</TableLayout>
</RelativeLayout>
ListView listView = getListView();
closeButtonView = getLayoutInflater().inflate(R.layout.closebutton_layout, listView, false);
closeButton = (Button)closeButtonView.findViewById(R.id.closebutton);
closeButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
closeQuestion();
}
});
listView.addFooterView(closeButtonView);
Does it have to be a footer and scroll with the list? Otherwise you could just add it to the bottom in your XML layout, e.g.:
<LinearLayout
android:orientation="vertical"
android:weightSum="1">
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_weight="1" />
<Button
android:id="@+id/btn_close"
... />
</LinearLayout>
Alternatively, does placing your Button inside a LinearLayout, then adding that as a footer help?
Edit:
My brain really wasn't switched on earlier.
You can define the footer in a separate layout XML file, then inflate that and add it as your footer as normal. e.g.:
ListView lv = getListView();
View footer = getLayoutInflater().inflate(R.layout.list_footer, lv, false);
lv.addFooterView(footer);
精彩评论