0

当 m 尝试从 java 代码(动态)在表布局中添加一行时。在这里阅读了许多问题,但无法找到解决方案。xml布局是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:context=".PlannedDeliveries" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="#C0C0C0"
        android:gravity="center_horizontal"
        android:text="Store ID:1245 Store:NYC"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TableLayout
        android:id="@+id/tablePD1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="30dp"
        android:background="#E0E0E0"
        android:orientation="vertical" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/table1"
            android:layout_centerHorizontal="true"
            android:background="#E0E0E0" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="100dip"
                android:layout_height="wrap_content"
                android:gravity="left"
                android:padding="5dip"
                android:text="Doc No" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="left"
                android:padding="5dip"
                android:text="Doc
Type" />

            <TextView
                android:id="@+id/textView4"
                android:layout_width="120dip"
                android:layout_height="wrap_content"
                android:gravity="left"
                android:padding="5dip"
                android:text="Source" />

            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="left"
                android:padding="5dip"
                android:text="Qty" />
        </TableRow>
    </TableLayout>

</RelativeLayout>

和java代码是

Log.v(LOG_TAG, "in create table row method");
        TableLayout tlPD = (TableLayout) findViewById(R.id.tablePD1);

        /*tlPD.removeView(tlPD);
        Log.v(LOG_TAG, "removing the view from table");
        */
        Log.v(LOG_TAG, "before adding values to table row first in method");
        //adding 1st  row in table



        //1st row,,,,

        TableRow trPD = new TableRow(this);
        LayoutParams lp1 = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        trPD.setLayoutParams(lp1);
        TextView r1c1 = new TextView(this);
        r1c1.setLayoutParams(lp1);
        r1c1.setBackgroundColor(getResources().getColor(R.color.lightGray));
        r1c1.setText(Html.fromHtml("3000000088"+"<br>"));
        TextView r1c2 = new TextView(this);
        r1c2.setLayoutParams(lp1);
        r1c2.setBackgroundColor(getResources().getColor(R.color.lightGray));
        r1c2.setText(Html.fromHtml("Tsf "+" <br>"));
        TextView r1c3 = new TextView(this);
        r1c3.setLayoutParams(lp1);
        r1c3.setBackgroundColor(getResources().getColor(R.color.lightGray));
        r1c3.setText(Html.fromHtml("Store ID:1247" + "<br> (Kingston Store) " ));

        TextView r1c4 = new TextView(this);
        r1c4.setLayoutParams(lp1);
        r1c4.setBackgroundColor(getResources().getColor(R.color.lightGray));
        r1c4.setText(Html.fromHtml("4"+"<br>"));
        trPD.addView(r1c1);
        trPD.addView(r1c2);
        trPD.addView(r1c3);
        trPD.addView(r1c4);

        Log.v(LOG_TAG, "row1 coloumn1" +r1c1);
        Log.v(LOG_TAG, "row1 coloumn2" +r1c2);
        Log.v(LOG_TAG, "row1 coloumn3" +r1c3);
        Log.v(LOG_TAG, "row1 coloumn4" +r1c4);



        tlPD.addView(trPD, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));


        Log.v(LOG_TAG, "after adding values to table row first in method");
        Log.v(LOG_TAG, "before adding values to table row second in method");

        //adding 2nd  row in table
        TableRow trPD1 = new TableRow(this);
        LayoutParams lp2 = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        trPD1.setLayoutParams(lp2);
        TextView r2c1 = new TextView(this);
        r2c1.setLayoutParams(lp2);
        r2c1.setBackgroundColor(getResources().getColor(R.color.lightGray));
        r2c1.setText(Html.fromHtml("4568"+"<br>"));

        TextView r2c2 = new TextView(this); 
        r2c2.setLayoutParams(lp2);
        r2c2.setBackgroundColor(getResources().getColor(R.color.lightGray));
        r2c2.setText(Html.fromHtml("PO "+" <br>"));
        TextView r2c3 = new TextView(this);
        r2c3.setLayoutParams(lp2);
        r2c3.setBackgroundColor(getResources().getColor(R.color.lightGray));
        r2c3.setText(Html.fromHtml("Supp ID:2346" + "<br> (Sony Corp) " ));
        TextView r2c4 = new TextView(this);
        r2c4.setLayoutParams(lp2);
        r2c4.setBackgroundColor(getResources().getColor(R.color.lightGray));
        r2c4.setText(Html.fromHtml("5"+"<br>"));
        trPD1.addView(r2c1);
        trPD1.addView(r2c2);
        trPD1.addView(r2c3);
        trPD1.addView(r2c4);

        tlPD.addView(trPD1, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        Log.v(LOG_TAG, "row2 coloumn1" +r2c1);
        Log.v(LOG_TAG, "row2 coloumn2" +r2c2);
        Log.v(LOG_TAG, "row2 coloumn3" +r2c3);
        Log.v(LOG_TAG, "row2 coloumn4" +r2c4);

        Log.v(LOG_TAG, "after adding values to table row second in method");

请帮我解决有点卡住..thnx 提前。

4

0 回答 0