2013-04-04 112 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