Android 表的包含XML布局中的ID
如果我计划单独动态更新单元格,我应该如何实现一个15×15单元格的表Android 表的包含XML布局中的ID,android,xml,Android,Xml,如果我计划单独动态更新单元格,我应该如何实现一个15×15单元格的表 我开始在main.xml中创建表,但我不想在一个代码体中用15个TableRow元素和每个元素中的15个元素来描述表。我觉得即使把桌子放在一个单独的文件里也太大了。可以创建行文件和包含行的表文件15次,并为每个单元格动态分配不同的ID?您可以执行如下操作: public void generateTable() { TableLayout tv = new TableLayout(context); for
我开始在main.xml中创建表,但我不想在一个代码体中用15个TableRow元素和每个元素中的15个元素来描述表。我觉得即使把桌子放在一个单独的文件里也太大了。可以创建行文件和包含行的表文件15次,并为每个单元格动态分配不同的ID?您可以执行如下操作:
public void generateTable() {
TableLayout tv = new TableLayout(context);
for (int y = 0; y < 15; y++) {
TableRow tr = new TableRow(context);
for (int x = 0; x < 15; x++) {
TextView tv = new TextView(context);
tr.addView(tv);
tv.setText("ID: " + (y*15 + x));
tv.setId(y*15 + x);
// And to allow for easy reading
if (y*15+x % 2 == 0)
tv.setBackgroundColor(0xffff0000);
else
tv.setBackgroundColor(0xff0000ff);
}
}
}
public void generateTable(){
TableLayout tv=新的TableLayout(上下文);
对于(int y=0;y<15;y++){
TableRow tr=新的TableRow(上下文);
对于(int x=0;x<15;x++){
TextView tv=新的TextView(上下文);
tr.addView(电视);
tv.setText(“ID:”+(y*15+x));
tv.setId(y*15+x);
//为了便于阅读
如果(y*15+x%2==0)
tv.setBackgroundColor(0xffff0000);
其他的
电视背景色(0xff0000ff);
}
}
}
您可以执行以下操作:
public void generateTable() {
TableLayout tv = new TableLayout(context);
for (int y = 0; y < 15; y++) {
TableRow tr = new TableRow(context);
for (int x = 0; x < 15; x++) {
TextView tv = new TextView(context);
tr.addView(tv);
tv.setText("ID: " + (y*15 + x));
tv.setId(y*15 + x);
// And to allow for easy reading
if (y*15+x % 2 == 0)
tv.setBackgroundColor(0xffff0000);
else
tv.setBackgroundColor(0xff0000ff);
}
}
}
public void generateTable(){
TableLayout tv=新的TableLayout(上下文);
对于(int y=0;y<15;y++){
TableRow tr=新的TableRow(上下文);
对于(int x=0;x<15;x++){
TextView tv=新的TextView(上下文);
tr.addView(电视);
tv.setText(“ID:”+(y*15+x));
tv.setId(y*15+x);
//为了便于阅读
如果(y*15+x%2==0)
tv.setBackgroundColor(0xffff0000);
其他的
电视背景色(0xff0000ff);
}
}
}
从xml创建这样的表布局
TableLayout tl = (TableLayout) findViewById(R.id.content_table);
您可以在xml中创建tablerow.xml表格行。这将帮助您更轻松地定义表格行布局。我只添加1个TextView
<TableRow xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="@+id/content"
android:singleLine="false"
/>
LayoutInflater充气机=getLayoutInflater();
对于(int-rowID=0;rowID<15;rowID++){
TableRow row=(TableRow)充气器。充气(R.id.TableRow,tl,false);
TextView content=(TextView)row.findViewById(R.id.content);
content.setId(rowID);
content.setText(“这是内容”);
tl.addView(行);
}
从xml创建这样的表布局
TableLayout tl = (TableLayout) findViewById(R.id.content_table);
您可以在xml中创建tablerow.xml表格行。这将帮助您更轻松地定义表格行布局。我只添加1个TextView
<TableRow xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="@+id/content"
android:singleLine="false"
/>
LayoutInflater充气机=getLayoutInflater();
对于(int-rowID=0;rowID<15;rowID++){
TableRow row=(TableRow)充气器。充气(R.id.TableRow,tl,false);
TextView content=(TextView)row.findViewById(R.id.content);
content.setId(rowID);
content.setText(“这是内容”);
tl.addView(行);
}
对于何时使用布局以及何时以编程方式构建布局,人们普遍接受的指导原则是什么?实际上没有什么指导原则。试着把它弄干净。什么时候使用布局,什么时候以编程方式构建布局,人们普遍接受的指导原则是什么?没有什么真正的指导原则。尽量把它弄干净。