写了一个函数,实现调用自定义的dialog,监听的点击事件,可以适当加入,这里实现针对ok的点击处理,主要传入自定义的view的布局和宽高。
private void showTipDialog(Context context, int resId, int widthDp, int heightDp) {
View view = LayoutInflater.from(context).inflate(resId, null);
final Dialog dialog = new Dialog(context, R.style.dialog);
dialog.setContentView(view);
TextView sure = (TextView) view.findViewById(R.id.sure);
sure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
finish();
}
});
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
dialog.show();
if (dialog.getWindow() != null) {
WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width = (int) (widthDp * (this.getResources().getDisplayMetrics().density + 0.5f));
params.height = (int) (heightDp * (this.getResources().getDisplayMetrics().density + 0.5f));
dialog.getWindow().setAttributes(params);
}
}
<style name="dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:background">@android:color/transparent</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:backgroundDimAmount">0.6</item>
</style>