سؤال

I created a popup without using xml. I have 4 buttons and I need them to be in a custom shape. Can somebody give me an example of how to do that?

This is the popup implementation:

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;
        }
    }

}

And this is the shape I want to use:

<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>
هل كانت مفيدة؟

المحلول

Add the xml under drawable and set as a background to the popup layout like this

layoutOfPopup.setBackgroundResource(R.drawable.button_shape);

And set a size to the drawable xml

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

and make the buttons size as WRAP_CONTENT or any fixed size

popRecord.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top