-1

我想创建一个可以动态显示LinearLayoutLinearLayout我的Android应用程序中指定区域的方法,这个线性布局的数量根据给定的数量而变化。

此外,在每个LinearLayout我想显示 aButton并在其下方显示 a TextView

这是我已经创建的方法:

public void putLinearLayout(double number){

       int mButtonHeight = 100;
        int mButtonWidth = 80;
         LinearLayout Linear = (LinearLayout)findViewById(R.id.linearlayout1);

        for(int i=1;i<=number;i++)
        {
            LinearLayout L = new LinearLayout(this);


            Button b= new Button(this);
            TextView tv = new TextView(this);
            L.setOrientation(LinearLayout.HORIZONTAL);
            b.setWidth(mButtonWidth);
            b.setHeight(mButtonHeight);
            L.addView(b);
            L.addView(tv);
            Linear.addView(L);
        }

    }
4

2 回答 2

0

我用它来绘制表格行:

public void createTableRow(String textBoxContent,Integer catId) {
      TableLayout tl = (TableLayout) findViewById(R.id.SelectCat);
      TableRow tr = new TableRow(this);
      LayoutParams lp = new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
      tr.setLayoutParams(lp);
      tr.setGravity(Gravity.RIGHT);

      Button btnLeft = new Button(this);
      btnLeft.setLayoutParams(new LayoutParams(0,android.view.ViewGroup.LayoutParams.WRAP_CONTENT,(float) 0.5));
      btnLeft.setText(R.string.Show);
      btnLeft.setTag(catId);
      TextView tvCenter = new TextView(this);
      tvCenter.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1f));
      tvCenter.setBackgroundColor(Color.WHITE);
      tvCenter.setText(textBoxContent);

      tr.addView(btnLeft);
      tr.addView(tvCenter);

      tl.addView(tr, new TableLayout.LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
    }


如果您有问题,更改为线性布局很简单,我可以指导您更多

于 2013-07-09T10:04:36.163 回答
-1

你只需要设置layoutparams

public void putLinearLayout(double number){
        LinearLayout Linear = (LinearLayout)findViewById(R.id.linearlayout1);
               int mButtonHeight = 100;
                int mButtonWidth = 80;

                for(int i=1;i<=number;i++)
                {
                    LinearLayout L = new LinearLayout(this);
                    L.setBackgroundColor(Color.WHITE);
                    LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //<--this line , is what you were missing
                    L.setLayoutParams(params); //<-- and then this
                    Button b= new Button(this);
                    b.setText(i+"");
                    TextView tv = new TextView(this);
                    tv.setText("i am textview number: "+i);
                    L.setOrientation(LinearLayout.HORIZONTAL);
                    b.setWidth(mButtonWidth);
                    b.setHeight(mButtonHeight);
                    L.addView(b);
                    L.addView(tv);
                    Linear .addView(L);
                }

}

于 2013-07-09T10:10:35.060 回答