【问题标题】:How can I programmatically set this tablelayout to have equal column widths for Android?如何以编程方式将此表格布局设置为具有相同的 Android 列宽?
【发布时间】:2016-02-22 22:19:20
【问题描述】:

我正在尝试以编程方式创建表格标题下方的行,但它们溢出了,我不确定如何将它们拆分为每个只占用 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" />

有人有解决这个问题的办法吗?

【问题讨论】:

  • 你也可以使用带权重的线性布局
  • 可能你不需要使用TableLayout。这个android:layout_weightLinearLayout 上工作得更好。

标签: java android android-tablelayout


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-07
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 2011-10-11
    • 2016-02-24
    • 1970-01-01
    相关资源
    最近更新 更多