如何以编程方式将此tablelayout设置为具有相同的Android列宽?

时间:2016-02-22 05:57:42

标签: java android android-tablelayout

我正在尝试以编程方式创建表格标题下方的行,但它们正在溢出,我不知道如何将它们拆分为仅占用每个宽度的25%?

以下是行创建的代码:

TableRow row = new TableRow(activity);
lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT, 1.0f);
tl = new TableLayout.LayoutParams();

tl.weight = 1;
lp.height = 100;

row.setLayoutParams(lp);
row.setWeightSum(4);


tv = new TextView(getActivity());
tv.setLayoutParams(tl);
tv.setTextSize(14);
tv.setText(whotos.get(i));

tv2 = new TextView(getActivity());
tv2.setLayoutParams(tl);
tv2.setTextSize(14);
tv2.setText(documents.get(i));

tv3 = new TextView(getActivity());
tv3.setLayoutParams(tl);
tv3.setTextSize(14);
tv3.setText(directions.get(i));

tv4 = new TextView(getActivity());
tv4.setLayoutParams(tl);
tv4.setTextSize(14);
tv4.setText(thedates.get(i));


 row.addView(tv);  //tp name
row.addView(tv2); //document type
row.addView(tv3); //direction
row.addView(tv4); //date

ll.addView(row, i + 1 + tableHeadingRows);

以下是我在编程创建的行上方的布局代码:

<TableRow
            android:id="@+id/tableRow2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="5dip"
            android:stretchColumns="1"
            android:weightSum="4">

            <TextView
                android:id="@+id/tp"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:text="TP"
                android:textAppearance="?android:attr/textAppearanceLarge" />
            <TextView
                android:id="@+id/doc"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:text="Doc"
                android:textAppearance="?android:attr/textAppearanceLarge" />
            <TextView
                android:id="@+id/dir"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:text="Direction"
                android:textAppearance="?android:attr/textAppearanceLarge" />
            <TextView
                android:id="@+id/date"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:text="Date"
                android:textAppearance="?android:attr/textAppearanceLarge" />
        </TableRow>

        <!-- draw a red line -->
        <View
            android:layout_height="2dip"
            android:background="@color/tableHorizontalRule" />

Image with columns stretching too far below headers, censored names and info

有没有人能解决这个问题?

1 个答案:

答案 0 :(得分:0)

我将其更改为LinearLayout,因为它简化了一些事情,并允许我以编程方式轻松设置列权重。