开发者

How can I dynamically create a button in Android? [duplicate]

开发者 https://www.devze.com 2023-01-02 18:27 出处:网络
This question alr开发者_运维技巧eady has answers here: Closed 10 years ago. Possible Duplicate: how to add button dynamically in android?
This question alr开发者_运维技巧eady has answers here: Closed 10 years ago.

Possible Duplicate:

how to add button dynamically in android?

How can I dynamically create a button in Android?


Firstly add the appropriate import to your Activity:

import android.widget.Button;

Then create a new button object within the onCreate method:

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

Finally add the button to the layout:

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

精彩评论

暂无评论...
验证码 换一张
取 消