如何在android中创建带边框的动态表

如何在android中创建带边框的动态表,android,android-layout,Android,Android Layout,我需要动态显示表边框和表行边框。我创建了如下表: TableLayout tl = (TableLayout) findViewById(R.id.tableLayout1); for (int i = initil; i <end; i++) { TableRow tr = new TableRow(this); tr.setTag(i); TableLayout.LayoutParams tableRowParams = n

我需要动态显示表边框和表行边框。我创建了如下表:

   TableLayout tl = (TableLayout) findViewById(R.id.tableLayout1);
    for (int i = initil; i <end; i++) {
        TableRow tr = new TableRow(this);
        tr.setTag(i);
        TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT);


        tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);

        TextView txtCode = new TextView(this);
        txtCode.setTextSize(1, 12);
        createView(tr, txtCode, productList.get(i).getProductCode());

        TextView txtDes = new TextView(this);
        txtDes.setTextSize(1, 12);
        createView(tr, txtDes, productList.get(i).getDescription());

        EditText txtQty = new EditText(this);
        txtQty.setTextSize(2, 12);
        txtQty.setHeight(4);
        txtQty.setWidth(6);
        txtQty.setId(i);
        txtQty.setFocusable(true);
        txtQty.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
        txtQty.setText("0.00");
        tr.addView(txtQty); 

        txtQty.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) { 
                Log.v("TAG", "afterTextChanged" + s.toString());
            }

            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                Log.v("TAG", "beforeTextChanged");
            }

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.v("TAG", "onTextChanged");
            }
        });

        if(invPriceEdit.equals("") && invPriceEdit.equals("1")){
            EditText txtPrice = new EditText(this);
            txtPrice.setText(Double.toString(productList.get(i).getPrice()));
            txtPrice.setTextSize(12);
            txtPrice.setHeight(3);
            txtPrice.setWidth(8);
            tr.addView(txtPrice); 

        }else{
            TextView txtPrice = new TextView(this);
            txtPrice.setTextSize(12);
            txtPrice.setWidth(8);
            txtPrice.setText(Double.toString(productList.get(i).getPrice()));
        }
        Log.i("----" , Double.toString(productList.get(i).getPrice()));


        tl.addView(tr);
                    tr.addView(t); 
    }

       public void createView(TableRow tr, TextView t, String viewdata) {
        t.setText(viewdata);
        t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        t.setTextColor(Color.DKGRAY);
        t.setPadding(1, 0, 0, 0);
        tr.setPadding(0, 0, 0, 0);
        tr.addView(t); 
}
TableLayout tl=(TableLayout)findViewById(R.id.tableLayout1);

对于(int i=initil;i您可以尝试定义一个样式xml文件,并使用setBackgroundDrawable()函数将其用于表

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#99FFFFFF"/>
    <corners android:radius="30px"/>
    <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" /> 
</shape>

一种简单的方法

  • 将黑色应用于整个表格布局
  • 对于每个表格行,应用1dp的边距
  • 对于表行中的每个子项,指定一个白色作为背景,并指定1dp的边距
  • 这将在行和列之间创建黑色边框

    <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/black" >
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="1dp"
                >
    
                <TextView
                    android:id="@android:id/empty"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                     android:layout_weight="1"
                     android:layout_marginLeft="1dp"
                     android:background="@android:color/white" 
                    android:text="@string/no_demos" />
    
                <TextView
                    android:id="@android:id/empty"
                     android:layout_weight="1"
                     android:layout_marginLeft="1dp"
                     android:background="@android:color/white" 
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="@string/no_demos" />
    
    
            </TableRow>
    
             <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="1dp"
               >
    
                <TextView
                    android:id="@android:id/empty"
                     android:layout_weight="1"
                      android:background="@android:color/white" 
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="@string/no_demos" />
    
                <TextView
                    android:id="@android:id/empty"
                     android:layout_weight="1"
                      android:background="@android:color/white" 
                     android:layout_marginLeft="1dp"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="@string/no_demos" />
    
    
            </TableRow>
    
    
        </TableLayout>
    

    这是不可能放置单杠的。是吗?