Android中的TableLayout的简单使用
在Layout中加入TableLayout控件。
<TableLayout android:id="@+id/myTableLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="157dp" >
</TableLayout>
Code
public class MainActivity extends Activity {
private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
private final int FP = ViewGroup.LayoutParams.FILL_PARENT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TableLayout myTableLayout = (TableLayout)this.findViewById(R.id.myTableLayout);
//全部列自动填充空白处
myTableLayout.setStretchAllColumns(true);
for(int r = 1; r < 10; r ++)
{
TableRow tr = new TableRow(this);
for(int c = 1; c < 5; c++)
{
TextView tv = new TextView(this);
tv.setText("Row " + r + " + Column " + c );
tr.addView(tv);
}
myTableLayout.addView(tr,new TableLayout.LayoutParams(FP, WC));
}
}
}
效果图