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

标签 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

如何在此处放置列边框。

最佳答案

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
        }

    }

关于android - 如何在android中以编程方式创建表格行和列边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21878768/

相关文章:

java - 在运行时传递参数 Dagger 2

java - 试图在表格布局中使表格行的高度相等

android - 在 Android 中更改设备方向时如何避免黑屏

android - 离线语音识别错误 ERROR_RECOGNIZER_BUSY

java - 响应监听器内部的字符串数组在监听器类外部提供空值

android - 如何创建正方形布局并将其水平居中

java - 即使具有 ArrayList 中的值,带有 TextView 的动态 TableLayout 也是空的

java - 如何为android数据网格设置id?

android - TableRow 内的 LinearLayout 不尊重分配的重力

android - 更新卡片 View 列表中的文本?