2010-04-03 66 views
2

在Android ListActivity中,我將一個按鈕添加爲ListView中的頁腳。如何將佈局格式應用於此按鈕,如居中和/或width = fill_parent?在Android ListActivity中,如何將佈局格式應用於頁腳?

我嘗試了一些東西,如使用setLayoutParams(),但還沒有得到它的工作 - 當我嘗試任何東西時,頁腳總是消失。

這裏是我與

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); 

*更新

基於克里斯托弗的回答工作的基本代碼,這裏是我加入到我的列表視圖頁腳觀點,並認爲增加了活動代碼它到列表視圖:

<?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); 

回答

3

是否必須是頁腳和滾動列表?否則,你可能只是把它添加到下在你的XML佈局,例如:

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

另外,不把你的按鈕的LinearLayout內,再加入作爲頁腳的幫助?


編輯:
我的腦子真不是在早期切換。

您可以在一個單獨的佈局XML文件中定義頁腳,然後將其填充爲正常頁腳並將其添加爲頁腳。例如:

ListView lv = getListView(); 
View footer = getLayoutInflater().inflate(R.layout.list_footer, lv, false); 
lv.addFooterView(footer); 
+0

感謝您的幫助。是的,它必須是頁腳和滾動列表 - 我不能一直佔用房地產。 至於你的第二個建議......因爲我必須以編程方式添加此頁腳,所以我不確定如何將它添加到LinearLayout中,然後將THAT佈局添加爲頁腳。那是怎麼做的? – JohnRock 2010-04-03 15:50:06

+0

對不起,我不知道我剛纔在說什麼(儘管你可以添加任何'View'作爲頁腳,所以'linearLayout.add(button)''可以做')。 無論如何,我已經用適當的解決方案更新了我的帖子。您可以使用良好的舊XML定義頁腳佈局,然後將其擴充並添加到頁腳中。 – 2010-04-03 20:43:12

+0

非常感謝您的更新 - 這正是我所錯過的。謝謝。我已經更新了我的問題,因爲我仍然對如何將OnClickListener指定給頁腳視圖中的Button的適當語法感到困惑,因爲它已嵌入到佈局中了......我是否正確設置了頁腳視圖? – JohnRock 2010-04-04 01:36:04