Android的动态创建按钮并填写布局 [英] Android Creating button dynamically and fill layout

查看:204
本文介绍了Android的动态创建按钮并填写布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我动态创建一个按钮。按钮的数目是依赖于数组列表的大小。问题是,在创建按钮后,我会添加到使用addview方法的布局。问题是我使用的线性布局,通过默认方向的线性布局是水平的,所以按钮将填补水平的布局。因为,一些按钮的是不可见的。我正在努力实现的东西看起来像这样

I'm creating a button dynamically. The number of button is depend on the size of arraylist. the problem is, after creating the button I will add to the layout using addview method. The problem is I'm using linear layout, as by default orientation for linear layout is horizontal, so the button will fill the layout horizontally. Because of that some of the button is not visible. What I'm trying to achieve is something look like this

我的code是象下面这样:

My code is like below:

Button[] tv = new Button[arraylist.size()];
for(int i=0;i<arraylist.size();i++){                                
    tv[i] = new Button(getApplicationContext());
    tv[i].setText(arraylist.get(i).toString());
    tv[i].setTextColor(Color.parseColor("#000000"));
    tv[i].setTextSize(20);
    tv[i].setPadding(15, 5, 15, 5);     
    linearlayout.addView(tv[i]);    
}

如果我设置线性布局的方向垂直的按钮,将填补垂直。因此,如果有任何解决方案,以动态地创建按钮并填写布局水平和垂直两个如图形象。

If I set the orientation of linear layout to vertical the button will fill vertically. So if there any solution to create the button dynamically and fill the layout both horizontal and vertical as shown by image.

推荐答案

经过2天苦苦思考布特这个问题终于我找到了解决办法。我一直尝试把我所有的联系人列表,将其存储在ArrayList和为每个元素创建按钮,我非常有显示在屏幕上后的结果满足。这里是我做的伎俩。我真的AP preciate从他人发表任何评论。

After 2 days struggling thinking bout this problem finally I've found the solution. I've try put all my contact list, store it in arraylist and create button for each element and I'm quite satisfy with the result after display on the screen. Here is how I do the trick. I really appreciate for any comment from others.

变量声明;

int currWidth;
int currCounter;
boolean isNewLine;
LinkedList<HashMap<String,Object>> button;
ArrayList<String> nameNumber = new ArrayList<String>();
contactWrapper = (LinearLayout) findViewById(R.id.multiple_selection);

Create按钮onClick事件;

create button onClick event;

for(int i=0;i<nameNumber.size();i++){                               
            tv[i] = new Button(getApplicationContext());                            
            String[] namePhone = nameNumber.get(i).toString().split("@@");
            phoneNumber.add(namePhone[1]);
            tv[i].setText(namePhone[0]);
            tv[i].setTag(namePhone[1]);
            tv[i].setTextColor(Color.parseColor("#000000"));                                
            tv[i].setTextSize(20);
            tv[i].setPadding(15, 5, 15, 5);                                 
            tv[i].measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            HashMap<String, Object> map = new HashMap<String,Object>();
            map.put("button", tv[i]);
            map.put("width", tv[i].getMeasuredWidth());                             
            button.add(map);
        }
        drawLayout();

drawlayout方法是我添加按钮,然后据此安排适合的布局;

drawlayout method is where I add button and arrange accordingly to fit the layout;

public void drawLayout(){
        int counter=0;
        contactWrapper.setOrientation(LinearLayout.VERTICAL);
        currCounter=0;
        currWidth=0;
        isNewLine=false;
        LinearLayout[] row = new LinearLayout[nameNumber.size()];
        row[currCounter] = new LinearLayout(getApplicationContext());
        @SuppressWarnings("rawtypes")       
        Iterator it = button.iterator();
        for(int i = 0; i<button.size(); i++){   
            it.next();  
            row[currCounter].setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
            currWidth += Integer.parseInt(button.get(i).get("width").toString());
            if(isNewLine){              
                if(currWidth < contactWrapper.getWidth()){  
                    row[currCounter].addView((View) button.get(i).get("button"));
                    if(!it.hasNext()){                      
                        contactWrapper.addView(row[currCounter]);
                    }else{                      
                        if(contactWrapper.getWidth()<(currWidth+Integer.parseInt(button.get(i+1).get("width").toString()))){
                            isNewLine=true;
                            contactWrapper.addView(row[currCounter]);
                            currCounter+=1; 
                            row[currCounter] = new LinearLayout(getApplicationContext());
                            currWidth=0;
                        }else{
                            isNewLine=false;
                        }
                    }
                }else{
                    isNewLine=true;
                    contactWrapper.addView(row[currCounter]);
                    currCounter+=1; 
                    row[currCounter] = new LinearLayout(getApplicationContext());
                    currWidth=0;
                }                                               
            }else{
                if(currWidth < contactWrapper.getWidth()){                                      
                    if(!it.hasNext()){
                        row[currCounter].addView((View) button.get(i).get("button"));
                        contactWrapper.addView(row[currCounter]);
                    }else{
                        row[currCounter].addView((View) button.get(i).get("button"));
                        if(contactWrapper.getWidth()<(currWidth+Integer.parseInt(button.get(i+1).get("width").toString()))){
                            isNewLine=true;
                            contactWrapper.addView(row[currCounter]);
                            currCounter+=1; 
                            row[currCounter] = new LinearLayout(getApplicationContext());
                            currWidth=0;
                        }else{
                            isNewLine=false;
                        }
                    }
                }else{
                    isNewLine=true;
                    contactWrapper.addView(row[currCounter]);
                    currCounter+=1; 
                    row[currCounter] = new LinearLayout(getApplicationContext());
                    currWidth=0;
                }
            }           
            counter++;
        }            
    }

这code相当混乱+我没有充分利用数组的大小

this code quite messy + I'm not fully utilize the size of array for

LinearLayout[] row = new LinearLayout[nameNumber.size()];

但它为我工作。

but it work for me.

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

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