如何创建Android的多个按钮 [英] How to Create multiple buttons in android

查看:151
本文介绍了如何创建Android的多个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过低于code编程,通过创建多个按钮,但它会创建一个单独的按钮,按我的应用程序需要创建一个基于输入的按钮。例如,如果输入的是3意味着我需要在布局来创建三个按钮。供您参考我重视样本图像和我的code。

I tried the below code to create multiple buttons through programatically but it creates a single button as per my application i need to create a button based on input. For example if the input is 3 means i need to create three buttons in a layout. For your reference i attached the sample image and my code.

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

推荐答案

如果在你的榜样,你的全球集装箱(main_layer)是一个RelativeLayout的或框架布局,将它们放置在海誓山盟的顶部。所以你不能看到后面顺序之一。

if in your example, your global container (main_layer) is a relativelayout or a frame layout, you have placed them on top of eachother. So you can't see the one on the back order.

试试这个PLS,

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

这篇关于如何创建Android的多个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆