gpt4 book ai didi

自定义形状的Android弹出窗口

转载 作者:行者123 更新时间:2023-11-30 02:57:16 25 4
gpt4 key购买 nike

我在没有使用 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>

最佳答案

drawable下添加xml,像这样设置为popup布局的背景

layoutOfPopup.setBackgroundResource(R.drawable.button_shape);

并为drawable xml设置一个大小

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

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

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

关于自定义形状的Android弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23070276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com