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

Posted

技术标签:

【中文标题】如何在android中以编程方式创建表格行和列边框【英文标题】:How to create table row and column border from programmatically in android 【发布时间】:2014-03-19 16:22:05 【问题描述】:

我关注 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:  android:color="#000000"/>
</shape>

我的输出:

如何在此处放置列边框。

【问题讨论】:

嘿,你能解释一下吗?我不明白你想在哪里显示列边框? 为每个单元格设置一个背景Drawable,九个补丁drawable是你的朋友 我将所有单元格设置在 cell_shape.xml 之上。但没有得到。看看我编辑的问题。 【参考方案1】:

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
        

    

【讨论】:

【参考方案2】:

这是我的解决方案:

table_divider.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="0dp" />
<solid android:color="#FFFFFF" />
    <stroke android: 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;

【讨论】:

以上是关于如何在android中以编程方式创建表格行和列边框的主要内容,如果未能解决你的问题,请参考以下文章

如何创建表格,创建行和列

DataFrame & Series

如何在表格上创建圆角并为整个表格和列保留表格边框? [复制]

C#DataGridView控件怎么增加行、列。

iPhone 应用程序编程中的行和列

如何通过分隔行和列来创建gridview?