2011-10-14 130 views
2

我在線性佈局中使用listview,我需要以編程方式添加按鈕。我嘗試了幾次tutiorials,沒有人足夠了,只是它沒有工作。你知道一些解決方案嗎?Android - 以編程方式將按鈕添加到ListView

編輯:我只是想要一個按鈕以編程方式添加和我的列表視圖(在列表視圖中沒有按鈕)。我無法以編程方式在我的活動中添加按鈕。

編輯:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
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_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/uss" 
/> 
<ListView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/list" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 

/> 

+0

凡在你的ListView做的添加按鈕,將其你需要添加按鈕?你需要爲ListView的每一行添加一個按鈕嗎?你的佈局xml也會有幫助。 – SBerg413

+0

您是否需要將按鈕放在每個ListView的行中? – SBerg413

+0

不,一個按鈕,然後是列表視圖 – Waypoint

回答

2

你將需要實現自定義適配器。您可以將按鈕添加到getView()方法內的ListView行中。那將是這個樣子:

public View getView(int position, View convertView, ViewGroup parent) { 

    View v; 
    if (convertView == null) 
     v = LayoutInflater.from(context).inflate(R.layout.row, parent, false); 
    else 
     v = convertView; 

    if(position == 0){ 
     Button button = new Button(); 
     v.add(button);  
    } 

    return v; 
} 

UPDATE:每提問者的回答我的問題評析,上述方案將投入ListView中的第一列的按鈕。

如果你想要做的是將按鈕放在列表視圖中以便它不滾動,那麼我會把另一個LinearLayout放在你剛纔包含TextView(id-uss)的現有Layout中。這樣您可以將按鈕添加到該LinearLayout的末尾。

0

如果您需要將按鈕添加到listview的每一行,那麼您需要去使用您指定的自定義列表項xml文件的自定義適配器。

,如果你需要你的ListView下面只有一個按鈕,你需要得到你已經添加在XML文件中的列表視圖的線性佈局,則可以使用mLinearLayout.add(mButton);

相關問題