Java 在Android运行时向LinearLayout添加新按钮
我想在运行时添加包含所有属性的新按钮,如下所示Java 在Android运行时向LinearLayout添加新按钮,java,android,Java,Android,我想在运行时添加包含所有属性的新按钮,如下所示 <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical"
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal" >
<Button
android:id="@+id/btnMM"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/android_btn_md"
android:gravity="center"
android:onClick="btnMMClick"
android:text="M-"
android:textColor="#000000"
android:textSize="25sp"
android:textStyle="bold" />
...... more buttons at design time are here .....
谢谢,
Ashok考虑到你是个新手:
Button myButton = new Button(context);
LinearLayout.LayoutParams lparms = new LinearLayout.LayoutParams(0,LayoutParams.FILL_PARENT);
lparms.weight = 1;
lparms.gravity = Gravity.CENTER;
myButton.setLayoutParams(lparms);
myButton.setBackground(getResources().getDrawable(R.drawable.android_btn_md));
myButton.setOnClickListener(btnMMClick);
myButton.setText("M-");
myButton.setTextColor(Color.parseColor("#000000"));
myButton.setTextSize(25);
myButton.setTypeface(null, Typeface.BOLD);
是什么阻止了你这么做?如果您尝试过这样做,请发布您的尝试。如果没有,那就去做,然后告诉我们你想出了什么。至少告诉我们你做了什么研究。LinearLayout布局=LinearLayout findViewByIdR.id.linearLayout1;按钮b=新按钮;b、 setTextM-;b、 设置宽度0度;b、 设置高度填充父对象;b、 设置权重1;这并不是试图在运行时添加一个新按钮。您可以在运行时将一个视图从XML膨胀到您的活动或片段所显示的当前视图……我总是用XML创建按钮,所以这是我第一次需要这样做。你提供了我想要的东西。非常感谢你,阿肖克