我想以编程方式进行此操作并添加表格行

时间:2013-05-29 05:18:20

标签: android xml android-layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/logo" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="322dp"
    android:text="TextView"
    android:background="@drawable/buttom_bg" />

我想以编程方式生成此布局并添加表格行

1 个答案:

答案 0 :(得分:3)

尝试此示例以编程方式在表格布局中包含三个表格行

    TableLayout tl=new TableLayout(this); 
    tl.setLayoutParams(new  TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
                                                  TableRow.LayoutParams.WRAP_CONTENT));
    tl.setStretchAllColumns(true);
    tl.setBackgroundResource(R.drawable.day);

    TableRow tr1=new TableRow(this);

    et1=new EditText(this);
    et1.setHeight(50);
    et1.setWidth(50);
    et1.setText("first row text box");

    b1=new Button(this);
    b1.setHeight(50);
    b1.setWidth(50);
    b1.setText("first row button");

    tr1.addView(et1);    
    tr1.addView(b1);
    tl.addView(tr1);

    TableRow tr2=new TableRow(this);
    et2=new EditText(this);
    et2.setHeight(50);
    et2.setWidth(50);
    et2.setText("second row text box");

    b2=new Button(this);
    b2.setHeight(50);
    b2.setWidth(50);
    b2.setText("second row button");
    b4=new Button(this);
    b4.setHeight(50);
    b4.setWidth(50);
    b4.setText("second row second button");
    tr2.addView(b4);
    tr2.addView(et2);
    tr2.addView(b2);

    tl.addView(tr2);
    TableRow tr3=new TableRow(this);

    et3=new EditText(this);
    et3.setHeight(50);
    et3.setWidth(50);
    et3.setText("third row text box");

    b3=new Button(this);
    b3.setHeight(50);
    b3.setWidth(50);
    b3.setText("third row button");
    b3.setGravity(android.view.Gravity.RIGHT);
    b3.setOnClickListener(this);
    tr3.addView(et3);
    tr3.addView(b3);

    tl.addView(tr3);

    setContentView(tl);