2
我最近开始使用Android玩arround。我试图实现的第一件事之一是创建一个动态的TableLayout。在网上搜索时,我发现了一些示例代码http://en.androidwiki.com/wiki/Dynamically_adding_rows_to_TableLayout,我将其复制到了我的活动中。动态添加行不可见
所以我的活动,现在看起来是这样的:
public class FooActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.foo);
/* Find Tablelayout defined in main.xml */
TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout);
for(int i= 0; i < 9; i++){
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.setBackgroundColor(Color.MAGENTA);
/* Create a Button to be the row-content. */
Button b = new Button(this);
b.setText("Dynamic Button");
b.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/* Add Button to row. */
tr.addView(b);
/* Add row to TableLayout. */
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
}
我在布局文件夹foo.xml看起来是这样的:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myTableLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:text="Static Button"/>
</TableRow>
</TableLayout>
当我现在开始我的应用程序在模拟器中,没有动态添加的行是可见的。我只看到了在foo.xml文件中定义的静态按钮。使用附加的调试器,我能够看到我的代码被执行,并且行被添加到tablelayout对象。但是,添加的行不会呈现。
任何想法?