如何以编程方式为简单的tablelayout添加边框

时间:2014-02-21 10:57:47

标签: android android-tablelayout

如何通过代码向TableLayout添加边框?

xml中的T​​ableLayout

<TableLayout 
            android:id="@+id/tableLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
</TableLayout>

我的代码

TableLayout prices = (TableLayout)findViewById(R.id.tableLayout1);
    prices.setStretchAllColumns(true);
    prices.bringToFront();
    for(int i = 0; i < 1; i++){
        TableRow tr =  new TableRow(this);
        TextView c1 = new TextView(this);
        c1.setText(equipHere);
        c1.setTextColor(Color.BLACK);
        c1.setTextSize(15);
        TextView c2 = new TextView(this);
        c2.setText("No of Days("+daysHere+")");
        c2.setTextColor(Color.BLACK);
        c2.setTextSize(15);
        tr.addView(c1);
        tr.addView(c2);
        prices.addView(tr);
    }

3 个答案:

答案 0 :(得分:3)

请执行以下操作:

    GradientDrawable gd=new GradientDrawable();
    gd.setStroke(2, Color.BLACK);
            prices.setBackgroungDrawable(gd);

答案 1 :(得分:2)

Gradient创建为res\xml\table.xml,例如:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient   android:startColor="#C0C0C0" 
            android:endColor="#505050"
            android:angle="90"/>   
 <corners android:radius="2px" />
</shape>

并设置为TableLayout背景

TableLayout table = (TableLayout)findViewById(R.id.tableLayout1);
table.setBackgroundDrawable(getResources().getDrawable(R.xml.table));

以编程方式,您可以实现:

  GradientDrawable gd = new GradientDrawable(
                GradientDrawable.Orientation.TOP_BOTTOM,
                new int[] {Color.parseColor("#C0C0C0"), Color.parseColor("#505050")});
        gd.setGradientCenter(0.f, 1.f);
        gd.setLevel(2);
        table.setBackgroundDrawable(gd);

答案 2 :(得分:0)

使用以下代码创建一个xml文件并放在drawable文件夹中     

<corners android:radius="0dp" />
<solid android:color="#FFFFFF" />
<stroke android:width="1sp" android:color="#e9e9e9" />
</shape>

并将xml设置为表格布局的可绘制的背景资源。