如何在android中以编程方式创建表行和列边框

时间:2014-02-19 11:10:34

标签: android android-tablelayout

我遵循此How can I create a table with borders in Android?这非常有用,但在我的情况下无法创建仅显示行边框的行边框。

我的代码:

TableLayout.LayoutParams tableLayoutParams = new     
TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,  TableLayout.LayoutParams.WRAP_CONTENT);
        TableLayout tableLayout = new TableLayout(getContext());
        tableLayout.setBackgroundColor(Color.WHITE);
        tableRowParams = new TableRow.LayoutParams(
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
        tableRowParams.setMargins(1, 1, 1, 1);
        //tableRowParams.weight = 1;

        for (i = 0; i < row + 1; i++) {
            tableRow = new TableRow(getContext());
            tableRow.setGravity(Gravity.CENTER_VERTICAL);
            //tableRow.setBackgroundColor(Color.BLACK);
            tableRow.setPadding(2, 2, 2, 2);
            tableRow.setBackgroundResource(R.drawable.cell_shape_new);
            for (j = 0; j < col + 1; j++) {
            LinearLayout ll = new LinearLayout(getContext());
                ll.setLayoutParams(new LayoutParams(
                        android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                        android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
                ll.setOrientation(1);
                TextView textView = new TextView(getContext());

                textView.setTextSize(25);
                textView.setTextColor(Color.BLACK);
                textView.setGravity(Gravity.CENTER);

                //Display required field
                }
                }
                }

R.drawable.cell_shape_new

    <?xml version="1.0" encoding="utf-8"?>
<shape
  xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape= "rectangle"  >
        <solid android:color="#FFFFFF"/>
        <stroke android:width="2dp"  android:color="#000000"/>
</shape>

我的输出:

enter image description here

如何在此处添加列边框。

2 个答案:

答案 0 :(得分:0)

TableRow行;

    shoppingCardDetails = new ArrayList<ShoppingCardDetails>();

    SoapObject courseOutlineObject = (SoapObject) ParserClass.ResponseVector
            .elementAt(3);

    final Vector courseOutline = (Vector) courseOutlineObject
            .getProperty(0);
    //System.out.println(courseOutline);
    //System.out.println("check record" + courseOutline.size());
    for (int current = 0; current < courseOutline.size(); current++) {

        try {

            row = (TableRow) LayoutInflater.from(this).inflate(
                    R.layout.table_row, null);
            chkbox = (CheckBox) row.findViewById(R.id.chkBox);
            chkbox.setId(current);

            ((TextView) row.findViewById(R.id.coursename))
                    .setText(((SoapObject) courseOutline.elementAt(current))
                            .getProperty("tocName").toString());

            ((TextView) row.findViewById(R.id.tvcourseprice))
                    .setText(((SoapObject) courseOutline.elementAt(current))
                            .getProperty("tocPrice").toString());

            /*
             * if ((((SoapObject) courseOutline.elementAt(current))
             * .getProperty("tocLevel") + "").equalsIgnoreCase("0")) {
             * 
             * ((LinearLayout) row.findViewById(R.id.llrow))
             * .setBackgroundColor(Color.parseColor("#F2F5A9"));
             * 
             * }
             */
            if ((((SoapObject) courseOutline.elementAt(current))
                    .getProperty("tocEnrollStatus") + "")
                    .equalsIgnoreCase("true")) {

                chkbox.setVisibility(View.INVISIBLE);
                ((TextView) row.findViewById(R.id.tvEnrolled))
                        .setVisibility(View.VISIBLE);
                ((TextView) row.findViewById(R.id.tvcourseprice))
                        .setVisibility(View.VISIBLE);

            }

            ((TableLayout) findViewById(R.id.course_outline_table))
                    .addView(row);

            chkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @overide
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {

                    try {
                        ShoppingCardDetails shoppingdetails = new ShoppingCardDetails();

                        if (isChecked) {

                            subTotal += 1;
                            total = subTotal;
                            shoppingdetails.name = ((SoapObject) courseOutline
                                    .elementAt(buttonView.getId()))
                                    .getProperty("tocName").toString();
                            shoppingdetails.price = ((SoapObject) courseOutline
                                    .elementAt(buttonView.getId()))
                                    .getProperty("tocPrice").toString();
                            shoppingCardDetails.add(shoppingdetails);

                        } else {
                            subTotal -= 1;
                            total = subTotal;

                            shoppingCardDetails.remove(total);

                        }

                        ((TextView) findViewById(R.id.btnRightHeader))
                                .setText("" + total);

                        ((TextView) findViewById(R.id.btnRightHeader))
                                .setPadding(30, 0, 0, 15);
                        ((TextView) findViewById(R.id.btnRightHeader))
                                .setTextColor(Color.WHITE);

                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                }
            });
        } catch (Exception e) {
            // TODO: handle exception
        }

    }

答案 1 :(得分:0)

这是我的解决方案: 1px bordered table cells

table_divider.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="0dp" />
<solid android:color="#FFFFFF" />
    <stroke android:width="1px" android:color="#AAAAAA" />
</shape>

任何对象列表的填充表:

private void fillTable(List<MenuItem> items) {
    TableLayout tl = (TableLayout)findViewById(R.id.main_content_button_container);
    TableRow tr = new TableRow(this);
    int count = 0;
    for (MenuItem item : items) {
        TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
        LinearLayout borderView = new LinearLayout(this);
        //We don't want to see double line... hiding top lines bigger then first row 
        int topMargin = count < 2 ? 1 : -1;
        if ((count%2) == 0) { //even
            tr = new TableRow(this);
            tableRowParams.setMargins(0,topMargin,0,0);
            borderView.setLayoutParams(tableRowParams);
            borderView.setBackgroundResource(R.drawable.table_divider);
            borderView.addView(getButton(item));
            tr.addView(borderView);
            tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
        } else { //odd
            tableRowParams.setMargins(-1,topMargin,0,0);
            borderView.setLayoutParams(tableRowParams);
            borderView.setBackgroundResource(R.drawable.table_divider);
            borderView.addView(getButton(item));
            tr.addView(borderView);
        }
        count++;
    }
}

细胞内容:

private Button getButton(NqMenuItem item) {
    int pixelValue = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 120, getResources().getDisplayMetrics());
    Button button = new Button(this);
    button.setText(item.title);
    button.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_drawer_about, 0, 0);
    TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, pixelValue);
    button.setBackgroundColor(Color.TRANSPARENT);
    button.setLayoutParams(tableRowParams);
    button.setPadding(0,30,0,0);
    return button;
}