Android - dialog自定义弹框样式

时间:2022-05-17 20:40:03

自定义dialog,有简单和单独重写的

效果图

Android - dialog自定义弹框样式

简单

Java代码:
Dialog dialog = new Dialog(this, R.style.DialogStyle);
dialog.setContentView(R.layout.start_dialog);
dialog.show();

Style:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="DialogStyle" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground"> @android:color/transparent </item> //中间框是透明的
<item name="android:backgroundDimEnabled">false</item>//四周有没有半透明黑色背景,false:没有,true:有
</style>
</resources>

重写dialog:




/**
* Created by zst on 2000/0/00.
*/

public class MapServiceDialog extends Dialog implements View.OnClickListener{
private Context mContext;

public MapServiceDialog(Context context) {
super(context);

this.mContext = context;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.dialog_map_service);
//this.getWindow().setBackgroundDrawable(new PaintDrawable(Color.TRANSPARENT));

//设置dialog属性
//setCancelable(true);
setCanceledOnTouchOutside(false);

}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_close://关闭本dialog
dismiss();
break;
}
}
}