目录
一、创建Dialog样式
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="TransparentDialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">#00000000</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item>
</style>
</resources>
二、AndoridManifest.xml中设置样式
<activity android:name="com.test.aaa.WebActivity"
android:theme="@style/TransparentDialog"/>
三、设置弹窗大小
@Override
protected void onCreate(Bundle savedInstanceState) {
//低版本上在圆角框下会有四角黑色问题,需设置window背景也透明
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
super.onCreate(savedInstanceState);
int screenWidth = NotchUtil.getRealWidth(getWindow().getWindowManager());
int screenHeight = NotchUtil.getRealHeight(getWindow().getWindowManager());
int webWidth, webHeight;
if (screenWidth < screenHeight){
//竖屏
webWidth = screenWidth * 4/5 ;
webHeight = screenHeight * 4/5 ;
}else {
//横屏
webWidth = screenWidth * 4/5 ;
webHeight = screenHeight * 4/5 ;
}
//填充布局,同时设置布局大小
setContentView(getWebPrivacyView(), new ViewGroup.LayoutParams(webWidth, webHeight));
}
四、设置弹窗以外区域点击不finish、以及返回键屏蔽
/**
* 弹窗以外区域点击事件屏蔽
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
return true;
}
/**
* 返回键屏蔽
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0;
}