Android Creating button dynamically and fill layout

前端 未结 6 1512
长情又很酷
长情又很酷 2021-01-01 05:25

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

6条回答
  •  醉梦人生
    2021-01-01 05:51

    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.

    variable declaration;

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

    create button onClick event;

    for(int i=0;i map = new HashMap();
                map.put("button", tv[i]);
                map.put("width", tv[i].getMeasuredWidth());                             
                button.add(map);
            }
            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

    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.

提交回复
热议问题