自定义形状的Android弹出窗口

时间:2014-04-14 20:58:23

标签: android android-xml shape android-popupwindow

我在不使用xml的情况下创建了一个弹出窗口。我有4个按钮,我需要它们是自定义形状。有人可以给我一个如何做到这一点的例子吗?

这是弹出式实现:

public class PopupAudio extends Activity implements OnClickListener {

    LinearLayout layoutOfPopup;
    PopupWindow popupMessage;
    Button popRecord, popStopRecord, popPlay, popStopPlaying;
    TextView popupText;

    Audio audio;

    public PopupAudio(Audio audio) {
        this.audio = audio;
    }

    public void showPopUp(View anchor) {
        popupMessage.showAsDropDown(anchor);
    }

    public void popupInit() {
        popRecord.setOnClickListener(this);
        popStopRecord.setOnClickListener(this);
        popPlay.setOnClickListener(this);
        popStopPlaying.setOnClickListener(this);
        popupMessage = new PopupWindow(layoutOfPopup, LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        popupMessage.setContentView(layoutOfPopup);
        //popupMessage.setBackgroundDrawable(R.drawable.popup_layout);
    }

    public void init(Context context) {
        popRecord = new Button(context);
        popRecord.setId(112);
        popStopRecord = new Button(context);
        popPlay = new Button(context);
        popStopPlaying = new Button(context);
        layoutOfPopup = new LinearLayout(context);
        popRecord.setText("REC");
        layoutOfPopup.setOrientation(1);
        layoutOfPopup.addView(popRecord);
        layoutOfPopup.addView(popStopRecord);
        layoutOfPopup.addView(popPlay);
        layoutOfPopup.addView(popStopPlaying);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()) {
        case 112:
            popRecord.setText("STOP");
            break;
        }
    }

}

这就是我想要使用的形状:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
  <corners
      android:topLeftRadius="0dp"
      android:topRightRadius="30dp"
      android:bottomRightRadius="30dp"
      android:bottomLeftRadius="30dp" />
  <stroke
      android:width="3dp"
      android:color="@android:color/background_dark" />
  <solid 
      android:color="#800000c0"/>
</shape>

1 个答案:

答案 0 :(得分:0)

drawable下添加xml,并将其设置为弹出式布局的背景,如下所示

layoutOfPopup.setBackgroundResource(R.drawable.button_shape);

并将大小设置为可绘制的xml

<size android:height="300dp" android:width="300dp"/>

并将按钮大小设为WRAP_CONTENT或任何固定大小

popRecord.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));