4

ボタンを動的に作成しています。ボタンの数はarraylistのサイズに依存します。問題は、ボタンを作成した後、addview メソッドを使用してレイアウトに追加することです。問題は、線形レイアウトのデフォルトの向きが水平であるため、線形レイアウトを使用しているため、ボタンがレイアウトを水平に埋めることです。そのため、一部のボタンが表示されません。私が達成しようとしているのは、このようなものです

Android メッセージング連絡先の複数のボタン

私のコードは以下のようなものです:

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

線形レイアウトの向きを垂直に設定すると、ボタンは垂直に塗りつぶされます。したがって、ボタンを動的に作成し、画像に示すように水平と垂直の両方のレイアウトを埋めるソリューションがある場合。

4

6 に答える 6

7

SDK には、まさに目的どおりのレイアウトが用意されているわけではありません (つまり、できるだけ多くの子を水平にレイアウトし、次の行に進んでさらにレイアウトします)。そのため、カスタムを作成する必要があります。ViewGroupそれはこの目的を達成します。幸運なことに、Devoxx でのプレゼンテーション中に、Romain Guy が 1 つのライブ画面を作成しました。

そのプレゼンテーション ビデオへのリンクは次のとおりです。

サンプル コードとスライドへのリンクは次のとおりです。

HTH

于 2012-01-15T15:46:20.090 に答える
6

この問題について考えるのに2日間苦労した後、ついに解決策を見つけました。すべての連絡先リストを入れて、それをarraylistに保存し、各要素のボタンを作成しようとしましたが、画面に表示された後の結果に非常に満足しています。これが私がトリックを行う方法です。他の方からのコメントは本当にありがたいです。

変数宣言;

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

ボタンの onClick イベントを作成します。

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 メソッドは、ボタンを追加し、レイアウトに合わせて配置する場所です。

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

このコードは非常に面倒です + 配列のサイズを十分に活用していません

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

しかし、それは私にとってはうまくいきます。

于 2012-01-18T01:58:33.643 に答える
0

これを試してみてください

this.row = (LinearLayout)findViewById(R.id.tags); this.row.setOrientation(LinearLayout.VERTICAL); LinearLayout one = new LinearLayout(this);

    //get the size of the screen
    Display display = getWindowManager().getDefaultDisplay();
    this.screenWidth = display.getWidth();  // deprecated
    this.screenHeight = display.getHeight();// depreceted

    for(int i=0; i<6; i++) {

        one.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        this.button = new Button(this);
        button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        if(i==0) {
            this.button.setText("Muhammad Aamir");
        } else if(i==1) {
            this.button.setText("Ahsan");
        } else if(i==2) {
            this.button.setText("Mujahid");
        } else if(i==3) {
            this.button.setText("Waqas");
        } else if(i==4) {
            this.button.setText("Ali");
        } else {
            this.button.setText("Ahmer");
        }

        //get the size of the button text
        Paint mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setTextSize(button.getTextSize());
        mPaint.setTypeface(Typeface.create(Typeface.SERIF, Typeface.NORMAL));
        float size = mPaint.measureText(button.getText().toString(), 0, button.getText().toString().length());
        size = size+14;
        this.totalTextWidth += size;

        if(totalTextWidth < screenWidth) {
            one.addView(button);
        } else {
            this.row.addView(one);
            one = new LinearLayout(this);
            one.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            one.addView(button);
            this.totalTextWidth = size;
        }
    }
    this.row.addView(one);
}
于 2013-05-01T10:08:54.700 に答える
0

TableLayoutこれの代わりに使用するのLinearLayoutチュートリアルです。これがアイデアを得るのに役立つことを願っています

于 2012-01-15T11:51:51.377 に答える
0

設定しますandroid:layout_width="fill_parent"か?そうでない場合はこれを行います。

于 2012-01-15T15:00:27.217 に答える
0

まあ、もっと洗練された方法を試すことができます。横一列のレイアウトを作成し、ボタンを追加できます。新しいボタンを追加しようとするたびに、レイアウトとボタンの幅の違いを見つけて、その場所があるかどうかを確認します。

水平レイアウトがいっぱいになるたびに、それを別の垂直レイアウトに追加し、別の水平レイアウトを作成してボタンを左に格納します。

私は自分のアプリでそのトリックを使用しました。

于 2012-01-15T15:47:17.027 に答える