我学会了如何使用XML文件创建UI。但请帮助我了解如何以编程方式执行此操作,而不使用XML文件,特别是对于LinearLayout以外的其他用户。以编程方式安装TableLayout
4
A
回答
14
使用下面的代码来创建TableLayout
TableLayout tbl=new TableLayout(context);
使用下面创建表行
TableRow tr=new TableRow(context);
添加视图到表行
tr.addView(view);
查看这里可能是一个TextView或EditText上或等..
添加表行到TableLayout
tbl.addView(tr);
就像你可以添加更多的表行到表布局。
4
下面的代码示例给出Here。
public class tablelayout extends Activity implements OnClickListener {
/** Called when the activity is first created. */
//initialize a button and a counter
Button btn;
int counter = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setup the layout
setContentView(R.layout.main);
// add a click-listener on the button
btn = (Button) findViewById(R.id.Button01);
btn.setOnClickListener(this);
}
// run when the button is clicked
public void onClick(View view) {
// get a reference for the TableLayout
TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);
// create a new TableRow
TableRow row = new TableRow(this);
// count the counter up by one
counter++;
// create a new TextView
TextView t = new TextView(this);
// set the text to "text xx"
t.setText("text " + counter);
// create a CheckBox
CheckBox c = new CheckBox(this);
// add the TextView and the CheckBox to the new TableRow
row.addView(t);
row.addView(c);
// add the TableRow to the TableLayout
table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
}
0
| * |表布局使用Java代码3×3个按钮:在tblRowCwtVal在tblColCwtVal
设置字符串
列的 集数排
集数|在tblAryVar中绘制
在这个例子中,我们使用了每个表格视图的按钮。您可以使用TextView | ImageView的和修改相应
int tblRowCwtVal = 3;
int tblColCwtVal = 3;
int[][] tblAryVar =
{
{R.drawable.ic_name, R.drawable.ic_name, R.drawable.ic_name},
{R.drawable.ic_name, R.drawable.ic_name, R.drawable.ic_name},
{R.drawable.ic_name, R.drawable.ic_name, R.drawable.ic_name}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.srn_nam_uic);
namRelLyoVar = (RelativeLayout) findViewById(R.id.NamSrnLyoUid);
TableLayout namTblLyoVar = new TableLayout(this);
TableLayout.LayoutParams tblLyoRulVar = new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams btnLyoRulVar = new TableRow.LayoutParams(50,50);
for(int tblRowIdxVar = 0; tblRowIdxVar < tblRowCwtVal; tblRowIdxVar++)
{
TableRow tblRowVar = new TableRow(this);
for(int tblColIdxVar = 0; tblColIdxVar < tblColCwtVal; tblColIdxVar++)
{
Button namIdxBtnVar = new Button(this);
Drawable DrwablIdxVar = getResources().getDrawable(tblAryVar[tblRowIdxVar][tblColIdxVar]);
DrwablIdxVar.setColorFilter(Color.rgb(0,128,0), PorterDuff.Mode.SRC_IN);
namIdxBtnVar.setBackground(DrwablIdxVar);
tblRowVar.addView(namIdxBtnVar, btnLyoRulVar);
}
namTblLyoVar.addView(tblRowVar, tblLyoRulVar);
}
namTblLyoVar.setLayoutParams(tblLyoRulVar);
namRelLyoVar.addView(namTblLyoVar);
}
相关问题
- 1. Android以编程方式创建TableLayout
- 2. 以编程方式创建TableLayout
- 3. 以编程方式滚动TableLayout
- 4. 以编程方式订购TableLayout列值
- 5. 以编程方式安装android事件
- 6. 安装Android APK以编程方式
- 7. 以编程方式安装字体
- 8. 以编程方式安装Android容器
- 9. 如何以编程方式安装.ipa
- 10. 以编程方式安装apk
- 11. 以编程方式安装Windows服务
- 12. 以编程方式安装.mobileconfig
- 13. 以编程方式安装Firefox插件
- 14. 以编程方式安装Windows服务
- 15. 以编程方式安装应用程序快捷方式
- 16. 以编程方式执行安装时出现安装失败
- 17. 在安装MyFbApp.apk之前以编程方式安装facebook.apk
- 18. 以编程方式安装在棉花糖上的APK安装
- 19. 可以以编程方式安装Windows Phone语言语言吗?
- 20. 以编程方式安装IIS7的更好方法
- 21. 以编程方式确定ClickOnce安装模式
- 22. 如何以编程方式安装iOS应用程序?
- 23. Android在后台以编程方式安装应用程序
- 24. 以编程方式从内存中安装应用程序
- 25. 以编程方式将程序集安装到GAC中
- 26. Bouncycastle以编程方式安装提供程序
- 27. 以编程方式从C#应用程序安装Apk文件
- 28. 如何以编程方式安装屏幕保护程序?
- 29. 以编程方式安装筛选器驱动程序?
非常感谢你..... –
它正在正常工作,但不显示活动画面上的任何输出......这里是我的代码 –
的LayoutParams PARAMS =新LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)\t \t TableLayout layout = new TableLayout(this); TableLayout.LayoutParams layoutparams = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT); TableRow tablerow = new TableRow(this); TextView tv = new TextView(this); tv.setText(“Demo for TableLayout”); tv.setLayoutParams(params); EditText et = new EditText(this); et.setHint(“Enter Text”); et.setLayoutParams(params); tablerow.addView(tv); tablerow.addView(et); layout.addView(tablerow); this.addContentView(layout,layoutparams); –