Android 如何在TableLayout中放置水平线?

Android 如何在TableLayout中放置水平线?,android,android-tablelayout,Android,Android Tablelayout,在我的表格布局中,我有几行,我想在它们之间画一条水平线,这样我就可以通过编程实现 TableLayout resultLayout = new TableLayout(this); resultLayout.setStretchAllColumns(true); resultLayout.setShrinkAllColumns(true); { // Code to create Registration mark Row

在我的表格布局中,我有几行,我想在它们之间画一条水平线,这样我就可以通过编程实现

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

这是我个人使用的

<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" />


如果希望以编程方式执行此操作,只需创建一个宽度与父视图相同、高度与所需高度相同的视图。

创建一个视图,并在完成每个表行后添加此视图。

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

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