LinearLayout根据给定的数字动态显示

时间:2013-07-09 09:45:22

标签: android android-linearlayout

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

此外,在每个LinearLayout我想要显示Button,并在其下方显示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);
        }

    }

2 个答案:

答案 0 :(得分: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));
    }

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

答案 1 :(得分:-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);
                }

}