基于Android代码实现常用布局

2019-12-10 19:54:32于丽

实现效果图:

基于Android代码实现常用布局

=========================================================================

 

 
  1. // 利用代码设置 表格布局  private void setTableLayout(){ 
  2. TableLayout tlayout = new TableLayout(this);  tlayout.setColumnStretchable(2, true); // 拉长索引从0开始的第2列 
  3. TableLayout.LayoutParams tl_lpara = new TableLayout.LayoutParams(MP,WC);  // 1. TableRow 不需要设置 layout_width, layout_height 
  4. // 2. TableRow 中的控件不能设置 layout_span 属性  TableRow tr1 = new TableRow(this);  
  5. TextView textView0 = new TextView(this);  textView0.setText("第0列"); 
  6. tr1.addView(textView0);   TextView textView1 = new TextView(this); 
  7. textView1.setText("第1列");  tr1.addView(textView1);  
  8. TextView textView2 = new TextView(this);  textView2.setText("第2列"); 
  9. textView2.setBackgroundColor(Color.CYAN);  tr1.addView(textView2);  
  10. tlayout.addView(tr1, tl_lpara);   TableRow tr2 = new TableRow(this); 
  11. Button btn0 = new Button(this);  btn0.setText("按钮0"); 
  12. tr2.addView(btn0);   Button btn1 = new Button(this); 
  13. btn1.setText("按钮1");  tr2.addView(btn1);  
  14. Button btn2 = new Button(this);  btn2.setText("按钮2"); 
  15. tr2.addView(btn2);   Button btn3 = new Button(this); 
  16. btn3.setText("按钮3");  tr2.addView(btn3); 
  17. tlayout.addView(tr2, tl_lpara);  setContentView(tlayout);