2013-04-04 65 views
1

我想在運行時在我的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空?

+0

添加幾個按鈕,如u需要並使之可見只有當你需要在運行時, – 2013-04-04 12:01:37

+1

謝謝@Ariu,但我不' t認爲這是一個優雅的解決方案 – GVillani82 2013-04-04 12:03:18

回答

1

我認爲你的問題是,你正在使用您的TextView發錯LayoutParams對象試試這個:

TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT); 

,然後將該layoutParams對象:

b.setLayoutParams(layoutParams); 
+0

你是對的@Emil。現在它工作了!謝謝!:) – GVillani82 2013-04-04 12:10:36

+0

歡迎你,有一個很好的編碼:) – 2013-04-04 12:11:27

-1

首先你需要使用此語句

BTN =(按鈕)findViewById(R.id.Button1)找到按鈕;

比你可以使用設置知名度這樣的:

btn.setVisibility(View.INVISIBLE);

然後你可以使用語句設置的可見性:

btn.setVisibility(View.VISIBLE);

+0

他動態添加,然後他將如何獲得ID? – 2013-04-04 12:03:32

+0

@Ravi當然不能使用id! – GVillani82 2013-04-04 12:07:06

0

代替你應該使用相對佈局的表格佈局,這對我來說是可行的嘗試

在你的XML文件中像這樣

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/myMainRelativeLayout" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<Button 
    android:id="@+id/btnAdd" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="47dp" 
    android:text="Add Button" /> 

而且Java代碼像波紋管

public class MainActivity extends Activity implements 
    android.view.View.OnClickListener { 

Button btnAdd; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    btnAdd = (Button) findViewById(R.id.btnAdd); 
    btnAdd.setOnClickListener(this); 

} 

@Override 
public void onClick(View v) { 
    if (v.getId() == R.id.btnAdd) { 
     RelativeLayout rl = (RelativeLayout)findViewById(R.id.myMainRelativeLayout); 
     Button btn = new Button(this); 
     btn.setId(1234); 
     btn.setText("Add Button Runtime"); 
     btn.setOnClickListener(this); 
     rl.addView(btn); 
    } 
    if(v.getId() == 1234) 
    { 
     Toast.makeText(getApplicationContext(), "This is Your New Button", Toast.LENGTH_LONG).show(); 

    } 
} 

}

試試這個它是真正的工作

+0

如果我的回答可以幫助你,請給我投票 – Bhadresh 2014-05-11 17:37:43

相關問題