自定义Dialog 实现弹出窗口

时间:2022-09-07 22:43:35

继承Dialog

public class SuccessSNDialog extends Dialog {
private TextView mShop = null;
private TextView mDetail = null;

public SuccessSNDialog(Context context, int themeResId) {
super(context, themeResId);
init(context);
}

private void init(Context context) {
setContentView(R.layout.dialog_success_sn);
if (context instanceof Activity) {
int screen_width = ((Activity) context).getWindowManager().getDefaultDisplay().getWidth();
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.width = (int) (screen_width * 0.8);
getWindow().setAttributes(lp);
}
mShop = (TextView) findViewById(R.id.shop);
mDetail = (TextView) findViewById(R.id.detail);
setCanceledOnTouchOutside(true);
TextView dismiss = (TextView) findViewById(R.id.dismiss);
dismiss.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SuccessSNDialog.this.dismiss();
}
});
}

public void setContents(String shop, String detail) {
mShop.setText(shop);
mDetail.setText(detail);
}

XML 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/dialog_bg"
android:paddingLeft="20dp"
android:paddingRight="20dp">


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:textAppearance="?android:textAppearanceLarge"
android:textColor="#ae8935"
android:text="序列号验证成功"/>


<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:src="@mipmap/verifyerial_succeed"/>


<TextView
android:id="@+id/shop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:textColor="#a9a9a9"
android:textAppearance="?android:textAppearanceMedium"/>


<TextView
android:id="@+id/detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:textColor="#a9a9a9"/>


<TextView
android:id="@+id/dismiss"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:gravity="center"
android:padding="8dp"
android:background="@drawable/button_bg"
android:textColor="#ffffff"
android:text="确 定"/>


</LinearLayout>

Style文件

  <style name="CustomizedDialogStyle" parent="android:style/Theme.Dialog">
<item name="android:background">#00ffffff</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
</style>

调用

  SuccessSNDialog successSNDialog = new SuccessSNDialog(this, R.style.CustomizedDialogStyle);
successSNDialog.setContents("蔻驰旗舰店", "满3000减300优惠券");
successSNDialog.show();