如何使两个表的列宽相等

时间:2018-03-09 16:11:26

标签: android android-tablelayout

这是表头:

<android.support.v4.widget.NestedScrollView
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:scrollbars="vertical"
            android:id="@+id/roster_bat_stats_table_header_adv1"
            android:scrollbarStyle="outsideInset"
            android:fillViewport="true"
            android:layout_gravity="top">
            <TableLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:divider="@drawable/divider_stand_table_list"
                android:showDividers="middle"
                android:isScrollContainer="true"
                android:stretchColumns="9"
                android:orientation="horizontal">
                <TableRow android:layout_width="match_parent"
                    android:id="@id/header_table_row"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:layout_weight="1">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:text="BAT"
                        android:background="@drawable/mybutton"
                        android:textAlignment="center"
                        android:textColor="@android:color/white"
                        android:layout_weight="0.0652"
                        android:textSize="18sp" />
                    etc...

这是没有标题的实际表格:

 <android.support.v4.widget.NestedScrollView
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:id="@+id/nestedScrollBatStatsAdv1"
            android:scrollbars="vertical"
            android:scrollbarStyle="outsideInset"
            android:fillViewport="true"
            android:layout_gravity="top">

            <TableLayout
                android:id="@+id/rosterBatStatsTableAdv1"
                android:layout_width="match_parent"
                android:visibility="gone"
                android:layout_height="match_parent"
                android:divider="@drawable/divider_stand_table_list"
                android:showDividers="middle"
                android:isScrollContainer="true"
                android:stretchColumns="9"
                android:orientation="horizontal">

            </TableLayout>

        </android.support.v4.widget.NestedScrollView>

我在上面的标题部分添加了android:layout_weight="0.0652"。 通过创建TableRow并为其中的每个元素设置权重来实际添加实际的表行:

double[] weigths   = new double[]{0.0893, 0.0893, 0.2500, 0.0833, 0.1012, 0.0952, 0.0893, 0.1071, 0.0952};

创建表格行:

TableRow tableRow = new TableRow(context);
    TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1);
    tableRow.setLayoutParams(lp);
    tableRow.setOrientation(TableRow.HORIZONTAL);
    tableRow.setBackgroundColor(resources.getColor(trainingPlayerSelectRowColor));
    tableRow.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
    tableRow.setTag(player.getId());

在tablerow中创建元素:

for (int i = 0; i < weights.length; ++i) {
        TextView h1 = new TextView(context);
        h1.setText(values[i]);
        TableRow.LayoutParams params = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, (float) weights[i]);
        h1.setLayoutParams(params);
        h1.setTextColor(resources.getColor(standingsTableHeadTXT));
        if (i == 2) {
            h1.setGravity(Gravity.LEFT);
        } else {
            h1.setGravity(Gravity.CENTER);
        }
        h1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        tableRow.addView(h1);
    }

values数组是一个字符串数组。

我在header_table_row中的权重(layout_weight)与在实际表中以编程方式添加的行相同。但他们不匹配:

enter image description here

是否有任何错误或如何使两个表中的列匹配?

0 个答案:

没有答案