我嘗試了下面的代碼通過編程方式創建多個按鈕,但它會根據我的應用程序創建一個按鈕,我需要根據輸入創建一個按鈕。例如,如果輸入是3意味着我需要在佈局中創建三個按鈕。爲了您的參考,我附上了示例圖片和我的代碼。如何在android中創建多個按鈕
for (int i = 0; i < array_of_btn_input.size(); i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);
Button button1 = new Button(getApplicationContext());
button1.setLayoutParams(params1);
button1.setText("button");
layout.addView(button1);
main_layer.addView(layout);
}
什麼類型來看主要佈局是什麼?這可能只是將按鈕堆疊在一起。 – BoredAndroidDeveloper
1)爲什麼每個按鈕都需要一個LinearLayout? – fiddler
2)什麼是全球集裝箱? – fiddler