我寫了一個簡單的應用程序,我想添加一些按鈕programmaticaly。問題是,它不知道要添加多少個按鈕。我試圖把「Button button = new Button」放到for循環中,因爲我只是創建了一個局部變量。我想這是我的錯;)添加未知數量的按鈕programmaticaly
這是我的代碼:
public class MainActivity extends Activity {
LinearLayout auswahl;
String element [] = new String [10]; //This is just an example, it would take many pages to show how this array gets created.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
auswahl = (LinearLayout)findViewById(R.id.LinearLayout2);
element [1] = "A";
element [2] = "B";
element [3] = "C";
element [4] = "D";
element [5] = "E";
element [6] = "F";
element [7] = "G";
element [8] = "H";
element [9] = "I";
element [0] = "J";
int anzahl = element.length;
for (int i = 0; i <= anzahl; i++){
schreibeButtons(i, element[i]);
}
}
public void schreibeButtons(int i, String string){
Button button = new Button(this);
button.setText(sortiment);
button.setWidth(auswahl.getWidth());
button.setHeight(40);
button.setId(i*100);
auswahl.addView(button);
} }
是否有什麼我想達到什麼問題嗎?有誰知道如何達到我的目標?謝謝你的幫助!
第一改變到'I
2013-03-27 18:19:13
男人我是這樣一個白癡 - .-非常感謝你!多數民衆贊成在;) – LosTheAlef 2013-03-27 18:20:32
@ρяσѕρєяK我在輸入相同的答覆兄弟..所以可以停止發佈它。 – Pragnani 2013-03-27 18:20:36