如何在android中创建多个按钮

时间:2012-11-15 15:08:37

标签: android

我尝试使用以下代码以编程方式创建多个按钮但是根据我的应用程序创建了一个按钮,我需要根据输入创建一个按钮。例如,如果输入为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);
}

enter image description here

4 个答案:

答案 0 :(得分:1)

如果在您的示例中,您的全局容器(main_layer)是relativelayout或框架布局,则将它们放在彼此的顶部。所以你看不到后面的那个。

试试这个,

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

答案 1 :(得分:0)

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

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

第二轮for循环创建一个新的线性布局,并在之前添加的布局

的顶部添加一个按钮

答案 2 :(得分: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(布局);

答案 3 :(得分: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设置为普通按钮。