可以用java代码完成这个静态按钮吗?

时间:2011-07-15 09:08:35

标签: java android button

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableTop="@drawable/randomImage"
    android:gravity="center_horizontal|bottom"
    />

特别是我在询问这两个属性:

android:drawableTop="@drawable/randomImage"
android:gravity="center_horizontal|bottom"

按钮的外观如下:

enter image description here

按钮使用静态xml完成​​。

最后,我必须回答我的问题:

android:gravity="center_horizontal|bottom"
记者:

setGravity(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM);

android:drawableTop="@drawable/randomImage"
记者:

setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom);

1 个答案:

答案 0 :(得分:1)

  Button mButton_dial = null;
     mButton_dial = new Button(this);
    mButton_dial.setText("Dial!");

    mLinearLayout_no_button = new LinearLayout(this);

    mLinearLayout_no_button.addView(mButton_dial);
    mButton_dial.setGravity(20);

    mButton_dial.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {

      }
    });