如何在动态表布局中将行宽设置为与标题行宽相同

时间:2016-02-11 09:10:50

标签: android width tablelayout android-layout-weight

我有一个像这样的动态表格布局:

<LinearLayout
    android:id="@+id/RelativeLayout1"
    android:layout_width="fill_parent"
    android:layout_gravity="center"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:padding="10sp"
    android:textColor="@color/accent_material_light"
    android:id="@+id/tabTitle"/>

            <TableLayout
                android:id="@+id/tableLayout1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:gravity="center_vertical"
                android:stretchColumns="*"
                android:shrinkColumns="*"
                android:layout_below="@+id/tabTitle"
                >
            </TableLayout>
        </LinearLayout>

现在我想将行宽设置为与动态表布局中的标题行宽相同。我想设置列值应根据不同的屏幕大小平均分配。

例如。我有4列说Id,名称,主题名称和主题标记。即使它们位于不同的屏幕尺寸设备中,这些值也应该是相等的空间。

但是这些列下面的值应该有各自的标题宽度。

  private void buildTable() {


        TableRow tr_head = new TableRow(getActivity());
     //   tr_head.setId(10);
        tr_head.setBackgroundColor(Color.GRAY);
        tr_head.setLayoutParams(new TableLayout.LayoutParams(
                TableRow.LayoutParams.MATCH_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT));

        tr_head.setGravity(Gravity.CENTER);
        tr_head.setWeightSum(4f);

        TextView label_id = new TextView(getActivity());
   //     label_date.setId(20);
        label_id.setText("ID");
        label_id.setTextColor(Color.WHITE);
        label_id.setPadding(5, 5, 5, 5);
        tr_head.addView(label_id);// add the column to the table row here

        TextView label_name = new TextView(getActivity());
        label_name .setText("Student Name"); // set the text for the header
        label_name .setTextColor(Color.WHITE); // set the color
        label_name .setPadding(5, 5, 5, 5); // set the padding (if required)
        tr_head.addView(label_name ); // add the column to the table row here

        TextView label_sub = new TextView(getActivity());
        label_sub .setText("Subject Name"); // set the text for the header
        label_sub .setTextColor(Color.WHITE); // set the color
        label_sub .setPadding(5, 5, 5, 5); // set the padding (if required)
        tr_head.addView(label_sub ); // add the column to the table row here

        TextView label_mark = new TextView(getActivity());
        label_mark.setText("Subject Mark"); // set the text for the header
        label_mark.setTextColor(Color.WHITE); // set the color
        label_mark.setPadding(5, 5, 5, 5); // set the padding (if required)
        tr_head.addView(label_mark); // add the column to the table row here

        tl.addView(tr_head, new TableLayout.LayoutParams(
                0,
                TableRow.LayoutParams.WRAP_CONTENT, 1f));

       .....

                        // Create the table row
                        TableRow tr = new TableRow(getActivity());
                        tr.setBackground(getResources().getDrawable(
                                R.drawable.row_color));
                        tr.setLayoutParams(new TableRow.LayoutParams(
                                0,
                                TableRow.LayoutParams.WRAP_CONTENT, 1f));

                        //Create two columns to add as table data

                        TextView labelid = new TextView(getActivity());
                        labelid .setText(id);
                        labelid .setPadding(2, 0, 5, 0);
                        labelid .setTextColor(Color.WHITE);
                        tr.addView(labelid );

                        TextView labelname = new TextView(getActivity());
                        labelname .setText(name);
                        labelname .setTextColor(Color.WHITE);
                        tr.addView(labelname );

                        TextView labelSub = new TextView(getActivity());
                        labelSub .setText(sub);
                        labelSub .setTextColor(Color.WHITE);
                        tr.addView(labelSub );

                        TextView labelSubMark = new TextView(getActivity());
                        labelSubMark .setText("mark");
                        labelSubMark .setTextColor(Color.WHITE);
                        tr.addView(labelSubMark);

                        // finally add this to the table row
                        tl.addView(tr, new TableLayout.LayoutParams(
                                0,
                                TableRow.LayoutParams.WRAP_CONTENT, 1f));

                    } while (c.moveToNext());
                }
            }
        }


    }

请帮助任何人......尝试了一切,但都是徒劳。

0 个答案:

没有答案