且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何编程创建一个按钮?

更新时间:2023-11-24 14:41:04

如果你想创建动态视图(如按钮,TextView的等),那么就使用这个code和在应用程序中运行它。

If you want to create dynamic view (like Button,textview etc) then just use this code and run it in your application.

MyActivity.java://your的Java文件

 LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1);
 Button btn = new Button(this)
 btn.setText("My Dynamic Button);
 btn.setMinLines(1);
 btn.setMaxLines(3);
 ll.addView(et);

XML文件中:

 <LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/TextView01"
android:layout_below="@+id/relativeLayout1"
android:orientation="vertical" >