修改布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".execl.ExeclVie"
android:orientation="vertical">
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="动态生成表格和边框"/>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:background="#DEDCD2">
<TableRow
android:background="#DEDCD2"
android:layout_margin="0.5dip">
<TextView
android:text="年度"
android:textSize="20dip"
android:textStyle="bold"
android:background="#FFFFFF"/>
<TextView
android:text="本金"
android:textSize="20dip"
android:textStyle="bold"
android:background="#FFFFFF"/>
<TextView
android:text="利息"
android:textSize="20dip"
android:textStyle="bold"
android:background="#FFFFFF"/>
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/TableLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:background="#DEDCD2">
<TableRow
android:background="#DEDCD2"
android:layout_margin="0.5dip">
<TextView
android:text=""
android:textSize="20dip"
android:textStyle="bold"
android:background="#FFFFFF"
android:layout_margin="1dip" />
<TextView
android:text=""
android:textSize="20dip"
android:textStyle="bold"
android:background="#FFFFFF"
android:layout_margin="1dip" />
<TextView
android:text=""
android:textSize="20dip"
android:textStyle="bold"
android:background="#FFFFFF"
android:layout_margin="1dip" />
</TableRow>
</TableLayout>
</LinearLayout>
添加资源文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#FFFFFF"
android:endColor="#FFFFFF"
android:angle="0"/>
<stroke
android:width="0.5dp"
android:color="#DEDCD2"/>
<corners
android:radius="2dp"/>
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp"/>
</shape>
修改主文件
package com.example.mpcc.mqttandroid.execl;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import com.example.mpcc.mqttandroid.R;
public class ExeclVie extends AppCompatActivity {
private final int wc = ViewGroup.LayoutParams.WRAP_CONTENT;
private final int FP = ViewGroup.LayoutParams.FILL_PARENT;
Button myButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_execl_vie);
myButton = (Button)findViewById(R.id.myButton);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TableLayout tableLayout = (TableLayout)findViewById(R.id.TableLayout01);
tableLayout.setStretchAllColumns(true);
for(int row=0;row<6;row++){
TableRow tableRow = new TableRow(ExeclVie.this);
tableLayout.setBackgroundColor(Color.rgb(222,220,210));
for(int col = 0;col<3;col++){
TextView tv = new TextView(ExeclVie.this);
tv.setBackgroundResource(R.drawable.shapee);
tv.setText("选择");
tableRow.addView(tv);
}
tableLayout.addView(tableRow,new TableLayout.LayoutParams(FP,wc));
}
}
});
}
}