如何在Android中动态添加按钮?

122

如何在 Android 中动态添加按钮?

18个回答

132
Button myButton = new Button(this);
myButton.setText("Push Me");

LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);

请查看此示例


1
我更新了URL,因为旧的链接返回404错误。请确认我引用的是正确的页面。 - Bill the Lizard
1
你能否完整地说明LayoutParams?我看到有超过12个带有这个名称的类。 - Saeed Neamati
1
@Saeed 在这个特定的例子中,它是属于 LinearLayout 的 LayoutParams。android.widget.LinearLayout.LayoutParams - Bassdrop Cumberwubwubwub

56

试试这个:

for (int i = 1; i <= 20; i++) {
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    Button btn = new Button(this);
    btn.setId(i);
    final int id_ = btn.getId();
    btn.setText("button " + id_);
    btn.setBackgroundColor(Color.rgb(70, 80, 90));
    linear.addView(btn, params);
    btn1 = ((Button) findViewById(id_));
    btn1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Toast.makeText(view.getContext(),
                    "Button clicked index = " + id_, Toast.LENGTH_SHORT)
                    .show();
        }
    });
}

1
为什么要先添加按钮,然后再获取它来设置点击监听器呢?难道不能先添加监听器,然后将其添加到布局中并完成吗? - Reese
最佳答案是因为您可以通过设置ID单击按钮 :) - boctulus

12

试试这个:

LinearLayout ll = (LinearLayout)findViewById(R.id.layout);

Button btn = new Button(this);
btn.setText("Manual Add");
btn.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
ll.addView(btn);

这里的R.id.layout是指什么?我一直收到未解决的layout错误。 - Anna Goldberg
@AnnaGoldberg 在你相关的xml文件中,应该定义一个LinearLayout。在这个摘录中,他们给他们的LinearLayout定义了一个名为layout的id,就像这样:android:id="@+id/layout"在他们的LinearLayout xml定义中。 - Amyga17

7
for (int k = 1; k < 100; k++) {
    TableRow row = new TableRow(this);

    innerloop:
    for (int l = 1; l < 4; l++) {
        btn = new Button(this);
        TableRow.LayoutParams tr = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layout.setWeightSum(12.0f);
        tr.weight = 0;
        btn.setLayoutParams(tr); 
        btn.setTextColor(a);
        btn.setHeight(150);

        btn.setWidth(150);
        btn.setId(idb);
        btn.setText("Button " + idb);
        row.addView(btn);
    }
}

6
Button btn = new Button(this);
btn.setText("Submit");
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.buttonlayout);
LayoutParams buttonlayout = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
linearLayout.addView(btn, buttonlayout);

6

试试这个

private void createLayoutDynamically(int n) {

    for (int i = 0; i < n; i++) {
        Button myButton = new Button(this);
        myButton.setText("Button :"+i);
        myButton.setId(i);
        final int id_ = myButton.getId();

        LinearLayout layout = (LinearLayout) findViewById(R.id.myDynamicLayout);
        layout.addView(myButton);

        myButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Toast.makeText(DynamicLayout.this,
                        "Button clicked index = " + id_, Toast.LENGTH_SHORT)
                        .show();
            }
        });
    }

5

尝试这段代码

 Button btn=new Button(this);
btn.setId(btn);
btn.setBackgroundResource(R.drawable.image);
btn.setMinimumHeight(150);
btn.setMinimumWidth(150);
Relativelayout.addView(btn); 

5

请检查这个。

LinearLayout ll_Main  = new LinearLayout(getActivity());
LinearLayout ll_Row01 = new LinearLayout(getActivity());
LinearLayout ll_Row02 = new LinearLayout(getActivity());

ll_Main.setOrientation(LinearLayout.VERTICAL);
ll_Row01.setOrientation(LinearLayout.HORIZONTAL);
ll_Row02.setOrientation(LinearLayout.HORIZONTAL);

final Button button01    = new Button(getActivity());
final Button button02    = new Button(getActivity());   
final Button button03    = new Button(getActivity());
final Button button04    = new Button(getActivity());

ll_Row01.addView(button01);
ll_Row01.addView(button02);

ll_Row02.addView(button03);
ll_Row02.addView(button04);

ll_Main.addView(ll_Row01);
ll_Main.addView(ll_Row02);

button04.setVisibility(View.INVISIBLE);
button04.setVisibility(View.VISIBLE);

4

请尝试这段代码。它将正常工作。

public class DynamicViewsActivity extends Activity {

Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_dynamic_views);
    ScrollView scrl=new ScrollView(this);
    final LinearLayout ll=new LinearLayout(this);
    ll.setOrientation(LinearLayout.HORIZONTAL);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(100, 500, 100, 200);
    scrl.addView(ll);
    Button add_btn=new Button(this);
    add_btn.setText("Click Here");

    ll.addView(add_btn, layoutParams);


    final Context context = this;

    add_btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent(context, App2Activity.class);
            startActivity(intent);
        }
    });
    this.setContentView(scrl);
}
}

3

试着使用以下代码。

LinearLayout layout = (LinearLayout) findViewById(R.id.llayout); 
layout.setOrientation(LinearLayout.VERTICAL);

Button btn = new Button(this);
btn.setText("Button1");

layout.add(btn);

btn = new Button(this);
btn.setText(Button2);
layout.add(btn);

像这样,您可以根据您的需求添加按钮。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接