1

我尝试了下面的代码以编程方式创建多个按钮,但它根据我的应用程序创建了一个按钮,我需要根据输入创建一个按钮。例如,如果输入为 3,则意味着我需要在布局中创建三个按钮。供您参考,我附上了示例图像和我的代码。

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

在此处输入图像描述

4

4 回答 4

1

如果在您的示例中,您的全局容器 (main_layer) 是相对布局或框架布局,则您已将它们放在彼此之上。所以你看不到延期交货的那个。

试试这个,

LinearLayout main_layer= (LinearLayout) findViewById(id.main_layer);


for (int i = 0; i < 10; 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);
}
于 2012-11-15T15:25:21.643 回答
0

您正在 for 循环中创建多个新的线性布局。需要将线性布局从 for 循环中取出,然后尝试在其中添加按钮。

第一次运行 for 循环创建一个新的线性布局并添加一个按钮

第二次运行 for 循环创建一个新的线性布局,并在之前添加的布局顶部添加一个按钮

于 2012-11-15T15:13:22.747 回答
0

在这里,您正在为每个按钮创建一个新的 LinearLayout...尝试更多类似...

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.WRAP_CONTENT,
    LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);

for (int i = 0; i < array_of_btn_input.size(); i++) {
    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);

    Button button1 = new Button(getApplicationContext());
    button1.setLayoutParams(params1);
    button1.setText("button");

    layout.addView(button1);

}

main_layer.addView(布局);

于 2012-11-15T15:15:55.150 回答
0

试试这个

LinearLayout llParent = (LinearLayout) findViewById(R.id.llParent);
llParent.removeAllViews();
for (int i = 0; i < array_of_btn_input.size(); i++) {
    BSView b = new BSView(this, new SimpleThumbBean(i + 1, bsList
            .get(i).getLabel(), bsList.get(i).getThumbUrl()));
    LinearLayout.LayoutParams llDynamic = new LinearLayout.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    llDynamic.weight = 1f;
    llParent.addView(b, llDynamic);
}

llParent 是一个已定义的布局,并确保在将布局动态添加到其中之前调用 removeAllViews()。

BSView 是我自己的自定义视图。您可以根据需要将 BSView 设置为普通 Button。

于 2012-11-15T15:19:08.350 回答