2011-10-13 48 views
0

這是我的活動代碼:Android的列表視圖通過頁腳菜單迭代太

@Override 
    public void onCreate(Bundle icicle) 
    { 
    super.onCreate(icicle); 
    String[] names = new String[] { "Investor Relations", "Social Sharing", "Rate this app", "About Rathbones", 
      "Disclaimer", "Security", "Credits"}; 
    // Create an ArrayAdapter, that will actually make the Strings above 
    // appear in the ListView 
    this.setListAdapter(new ArrayAdapter<String>(this, R.layout.moremenulist, 
      R.id.label, names)); 
    } 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 
     // Get the item that was clicked 
     Object o = this.getListAdapter().getItem(position); 
     String keyword = o.toString(); 
     Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG).show(); 
    } 

這裏是這個XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout android:id="@+id/LinearLayout1" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:id="@+id/label"></TextView> 
    <LinearLayout android:id="@+id/linearLayout2" android:layout_width="wrap_content" android:orientation="vertical" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentBottom="true"> 

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

     <ImageButton android:layout_width="wrap_content" android:id="@+id/imgbtn1" android:layout_alignParentLeft="true" android:layout_alignBottom="@+id/imgbtn3" android:layout_height="wrap_content" android:src="@drawable/topnews" android:visibility="visible"></ImageButton> 
     <ImageButton android:layout_width="wrap_content" android:layout_alignParentRight="true" android:id="@+id/imgbtn5" android:layout_alignBottom="@+id/imgbtn4" android:layout_height="wrap_content" android:src="@drawable/more"></ImageButton> 
     <ImageButton android:layout_width="wrap_content" android:id="@+id/imageButton1" android:layout_height="wrap_content" android:src="@drawable/contact_us" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/imgbtn1"></ImageButton> 

</RelativeLayout> 
    </LinearLayout> 



</RelativeLayout> 

現在,當活動被加載它是附加列表中的每個項目的菜單。我只希望它只出現在底部而不是每個列表項目。 有什麼建議嗎?

編輯-1:

我嘗試這樣做,但得到空指針異常

View footerView = 


    ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer, null, false); 
     ListView lv = new ListView(this); 
     lv.addFooterView(footerView); 

回答

1

的錯誤你是什麼意思的菜單?你的意思是你的ImageButtons?

如果是這種情況,請刪除那些爲這些ImageButton創建另一個佈局,並使用ListView.addFooterView()來代替它們。

您可以創建例如list_footer_layout.xml,其中包含您的ImageButtons。然後:

public void onCreate(Bundle bundle) { 
    ... 
    ListView lv = getListView(); // If your activity is ListActivity 
    View foot = getLayoutInflater().inflate(R.layout.list_footer_layout, null); 
    lv.addFooterView(foot); 
} 
+0

是的,我的意思是隻有imagebutton。我如何使用addfooterview方法? – Maverick

+0

nope,它仍然顯示每個元素的菜單。 – Maverick

+1

你可以發佈截圖和你當前的佈局文件嗎?你在使用ListActivity嗎? – user802421