如何在TableLayout中放置一条水平线?

时间:2013-09-16 11:29:57

标签: android android-tablelayout

在我的表格布局中,我有几行,我想在它们之间做一条水平线,所以我该怎样才能以编程方式进行。

   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);
}

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

答案 2 :(得分: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);
    }

希望它会对你有所帮助。