Android:如何动态创建这样的布局(不是通过xml)?

时间:2012-01-10 04:29:20

标签: android xml android-layout android-emulator xml-layout

在我的应用程序中,我想创建如下图所示的布局:

enter image description here

那么如何才能实现呢?

我做过类似下面的代码:

public void showResult()
{

    List<TextView> textListWord = new ArrayList<TextView>(tempEmployerList.size());
    List<TextView> textListAnswer = new ArrayList<TextView>(tempEmployerList.size());
    List<TextView> imageListAnswer = new ArrayList<TextView>(tempEmployerList.size());
    for(int i = 0; i<=tempEmployerList.size()-1; i++)
    {    
        LinearLayout innerLayout = new LinearLayout(this);
        innerLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        innerLayout.setBackgroundColor(0x00000000);

        // set the Multiple TextView
        TextView mHeading = new TextView(getApplicationContext());
        TextView middleValue = new TextView(getApplicationContext());
        TextView aImageView = new TextView(getApplicationContext());

        mHeading.setText("\n"+"1");
        //mHeading.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        mHeading.setTextColor(0xFFFF0000);
        mHeading.setPadding(3, 0, 0, 0);


        middleValue.setText("\n"+"2");
        //middleValue.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        middleValue.setTextColor(0xFFFF0000);
        middleValue.setGravity(Gravity.RIGHT);
        middleValue.setPadding(2, 0, 9, 0);

        aImageView.setText("\n"+"3");
        //aImageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        aImageView.setGravity(Gravity.RIGHT);
        aImageView.setTextColor(0xFF000000);
        aImageView.setPadding(0, 0, 9, 0);

        View line = new View(getApplicationContext());
        //line.setOrientation(1);
        line.setLayoutParams(new LayoutParams(2, android.view.ViewGroup.LayoutParams.FILL_PARENT)); 
        line.setBackgroundColor(0xFF000000); 

        /**** Any other text view setup code ****/    

        innerLayout.addView(mHeading);
        innerLayout.addView(middleValue);
        innerLayout.addView(aImageView);
        innerLayout.addView(line);
        myLinearLayout.addView(innerLayout);  

        textListWord.add(mHeading); 
        textListAnswer.add(middleValue);
        imageListAnswer.add(aImageView);

    } 
}

那么请指导我,我还需要做些什么来创建这样的视图? 感谢。

1 个答案:

答案 0 :(得分:1)

尝试TableLayout和TableRow而不是LinearLayout。

你也可以在xml中创建TableLayout,因为它,我认为是静态的并添加TableRows。要在Java中使用创建TableLayout,

TableLayout tblLayout=new TableLayout(this); 

在java中设置LayoutParams和表格布局的其他属性,就像我正在设置LayoutParams:

LayoutParams params=new LayoutParams(LayoutParams.Fill_Parent, LayoutParams.Wrap_Content); 
tblLayout.setLayoutParams(params);

为TableRows的创建和插入创建一个循环:

for(int i=0;i<arrList1.size();i++)
{
   TableRow row=new TableRow(this);
   row.setLayoutParams(params);

   TextView lbl1=new TextView(this);
   lbl1.setText(arrList1.get(i));
   row.addView(lbl1);

   TextView lbl2=new TextView(this);
   lbl2.setText(arrList2.get(i));
   row.addView(lbl2);

   TextView lbl3=new TextView(this);
   lbl3.setText(arrList3.get(i));
   row.addView(lbl3);

   tblLayout.addView(row);
}