我想在運行時在我的TableRow中添加一些按鈕。我的TableRow具有這樣的結構:Android:在運行時添加按鈕
<TableRow
android:layout_marginTop="100px"
android:gravity="bottom"
android:paddingTop="50px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/riga1">
<Button
android:textSize="32px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonOK2"
android:text="YES"
android:onClick="setResult"/>
</TableRow>
我的Java代碼:
TableRow tr = (TableRow) findViewById(R.id.riga1);
Button b = new Button(this);
b.setText("button 1");
b.setId(1);
b.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
tr.addView(b);
Button b1 = new Button(this);
b1.setText("button 2");
b1.setId(2);
b1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
tr.addView(b1);
但這似乎不工作。在我的活動中,我只能看到xml文件中定義的按鈕。
的另一個問題是,如果我在相關TableLayout設置參數:
android:stretchColumns="*"
我必須插入(在XML文件)在上述TableLayout的的TableRow至少一個按鈕,否則我得到「除零錯誤「附近streatchColumns我能做些什麼,爲了讓tableRow空?
添加幾個按鈕,如u需要並使之可見只有當你需要在運行時, – 2013-04-04 12:01:37
謝謝@Ariu,但我不' t認爲這是一個優雅的解決方案 – GVillani82 2013-04-04 12:03:18