Android:在运行时添加按钮

时间:2013-04-04 11:54:55

标签: android android-layout button view

我正在尝试在运行时在TableRow中添加一些按钮。我的TableRow有这样的结构:

    <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="*" 

我必须在上述TableLayout的TableRow中插入(在xml文件中)至少一个按钮,否则我在streatchColumns附近获得“除零错误”我该怎么做才能让tableRow为空?

3 个答案:

答案 0 :(得分:1)

我认为您的问题是您使用错误的LayoutParams对象TextView试试这个:

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

然后设置此layoutParams对象:

 b.setLayoutParams(layoutParams);

答案 1 :(得分: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代码如bellow

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();

    }
}

}

试试这个它真的有用

答案 2 :(得分:-1)

首先你需要使用这个声明找到按钮

  

BTN =(按钮)findViewById(R.id.Button1);

可以使用此设置可见性:

  

btn.setVisibility(View.INVISIBLE);

然后,您可以使用以下语句设置可见性:

  

btn.setVisibility(View.VISIBLE);

相关问题