0

在我的表格布局中,我有几行,我想在它们之间画一条水平线,那么我该如何以编程方式进行。

   TableLayout resultLayout = new TableLayout(this);
        resultLayout.setStretchAllColumns(true);
        resultLayout.setShrinkAllColumns(true);
{

            // Code to create Registration mark Row

            TableRow tableRowRegistrationMark = new TableRow(this);
            tableRowRegistrationMark.setGravity(Gravity.CENTER_HORIZONTAL);
            TextView textViewRegistartionMark = new TextView(this);
            textViewRegistartionMark.setText("Registration mark:");
            textViewRegistartionMark.setTextSize(TypedValue.COMPLEX_UNIT_DIP,
                    12);
            textViewRegistartionMark.setTypeface(Typeface.SERIF, Typeface.BOLD);
            textViewRegistartionMark.setGravity(Gravity.LEFT);
            TextView textViewRegistrationMarkData = new TextView(this);
            textViewRegistrationMarkData.setText(strbs[3]);
            textViewRegistrationMarkData.setTextSize(
                    TypedValue.COMPLEX_UNIT_DIP, 12);
            textViewRegistrationMarkData.setTypeface(Typeface.SERIF,
                    Typeface.BOLD);
            textViewRegistrationMarkData.setGravity(Gravity.LEFT);

            tableRowRegistrationMark.addView(textViewRegistartionMark, 0);
            tableRowRegistrationMark.addView(textViewRegistrationMarkData, 1);

            // Code to create Make Model Row

            TableRow tableRowMakeModel = new TableRow(this);
            tableRowMakeModel.setGravity(Gravity.CENTER_HORIZONTAL);
            TextView textViewMakeModel = new TextView(this);
            textViewMakeModel.setText("Make/Model:");
            textViewMakeModel.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
            textViewMakeModel.setTypeface(Typeface.SERIF, Typeface.BOLD);
            textViewMakeModel.setGravity(Gravity.LEFT);
            TextView textViewMakeModelData = new TextView(this);
            textViewMakeModelData.setText(strddss[1]);
            textViewMakeModelData.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
            textViewMakeModelData.setTypeface(Typeface.SERIF, Typeface.BOLD);
            textViewMakeModelData.setGravity(Gravity.LEFT);

            tableRowMakeModel.addView(textViewMakeModel, 0);
            tableRowMakeModel.addView(textViewMakeModelData, 1);

            // Code to create RowColour

            TableRow tableRowColour = new TableRow(this);
            tableRowColour.setGravity(Gravity.CENTER_HORIZONTAL);
            TextView textViewColour = new TextView(this);
            textViewColour.setText("Colour:");
            textViewColour.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
            textViewColour.setTypeface(Typeface.SERIF, Typeface.BOLD);
            textViewColour.setGravity(Gravity.LEFT);
            TextView textViewColourData = new TextView(this);
            textViewColourData.setText(strddss[2]);
            textViewColourData.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
            textViewColourData.setTypeface(Typeface.SERIF, Typeface.BOLD);
            textViewColourData.setGravity(Gravity.LEFT);

            tableRowColour.addView(textViewColour, 0);
            tableRowColour.addView(textViewColourData, 1);

            // Code to create RowChassisNo

            TableRow tableRowChasisNo = new TableRow(this);
            tableRowChasisNo.setGravity(Gravity.CENTER_HORIZONTAL);
            TextView textViewChasisNo = new TextView(this);
            textViewChasisNo.setText("VIN/Chassis No:");
            textViewChasisNo.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
            textViewChasisNo.setTypeface(Typeface.SERIF, Typeface.BOLD);
            textViewChasisNo.setGravity(Gravity.LEFT);
            TextView textViewChasisNoData = new TextView(this);
            textViewChasisNoData.setText(strddss[3]);
            textViewChasisNoData.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
            textViewChasisNoData.setTypeface(Typeface.SERIF, Typeface.BOLD);
            textViewChasisNoData.setGravity(Gravity.LEFT);

            tableRowChasisNo.addView(textViewChasisNo, 0);
            tableRowChasisNo.addView(textViewChasisNoData, 1);
resultLayout.addView(tableRowRegistrationMark);
            resultLayout.addView(tableRowMakeModel);
            resultLayout.addView(tableRowColour);
            resultLayout.addView(tableRowChasisNo);
            resultLayout.addView(tableRowDateofFirstUse);
            resultLayout.addView(tableRowTypeofFuel);
}
4

3 回答 3

0

这是我个人使用的。

<View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="1dp"
        android:background="@android:color/darker_gray" />

如果您希望以编程方式执行此操作,只需根据需要创建一个View宽度与父级和高度相同的。

于 2013-09-16T11:39:47.833 回答
0

只需将以下代码放在表格布局循环的顶部

TableLayout.LayoutParams td_tr = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
td_tr.topMargin = GetPixel(1, activityContext);

public int GetPixel(float f , Context context)
    {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return (int)((f * displayMetrics.density) + 0.5f);
    }

希望它会帮助你。

于 2013-09-16T11:56:45.847 回答
0

创建一个视图并在完成每个 tablerow 后添加此视图

于 2013-09-16T11:50:12.427 回答