优化
This commit is contained in:
@@ -3,14 +3,14 @@ package com.sw.dualscreen.view;
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.os.Build;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.view.Gravity;
|
||||
import android.view.View;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.sw.dualscreen.R;
|
||||
|
||||
/**
|
||||
@@ -18,78 +18,86 @@ import com.sw.dualscreen.R;
|
||||
*/
|
||||
public class CustomDialog extends Dialog {
|
||||
|
||||
|
||||
/**
|
||||
* 宽高由布局文件中指定(但是最底层的宽度无效,可以多嵌套一层解决)
|
||||
*/
|
||||
public CustomDialog(Context context, View layout) {
|
||||
super(context, R.style.LoadingDialog);
|
||||
setContentView(layout);
|
||||
initConfig();
|
||||
}
|
||||
|
||||
public CustomDialog(Context context, View layout, int style) {
|
||||
|
||||
super(context, style);
|
||||
|
||||
setContentView(layout);
|
||||
initConfig();
|
||||
}
|
||||
|
||||
private void initConfig() {
|
||||
setCancelable(true);
|
||||
setCanceledOnTouchOutside(true);
|
||||
Window window = getWindow();
|
||||
|
||||
WindowManager.LayoutParams params = window.getAttributes();
|
||||
|
||||
params.gravity = Gravity.CENTER;
|
||||
|
||||
window.setAttributes(params);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 宽高由该方法的参数设置
|
||||
*/
|
||||
public CustomDialog(Context context, int width, int height, View layout,
|
||||
int style) {
|
||||
super(context, style);
|
||||
// 设置内容
|
||||
setContentView(layout);
|
||||
// 设置窗口属性
|
||||
Window window = getWindow();
|
||||
WindowManager.LayoutParams params = window.getAttributes();
|
||||
// 设置宽度、高度、密度、对齐方式
|
||||
float density = getDensity(context);
|
||||
params.width = (int) (width * density);
|
||||
params.height = (int) (height * density);
|
||||
params.gravity = Gravity.CENTER;
|
||||
window.setAttributes(params);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取显示密度
|
||||
*
|
||||
* @param context
|
||||
* @return
|
||||
*/
|
||||
public float getDensity(Context context) {
|
||||
Resources res = context.getResources();
|
||||
DisplayMetrics dm = res.getDisplayMetrics();
|
||||
return dm.density;
|
||||
}
|
||||
|
||||
private void fullScreenImmersive(View view) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||
int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|
||||
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
|
||||
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
|
||||
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
|
||||
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|
||||
| View.SYSTEM_UI_FLAG_FULLSCREEN;
|
||||
view.setSystemUiVisibility(uiOptions);
|
||||
if (window != null) {
|
||||
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
|
||||
WindowManager.LayoutParams params = window.getAttributes();
|
||||
params.gravity = Gravity.CENTER;
|
||||
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
|
||||
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
|
||||
window.setAttributes(params);
|
||||
}
|
||||
|
||||
}
|
||||
//
|
||||
// /**
|
||||
// * 宽高由该方法的参数设置
|
||||
// */
|
||||
// public CustomDialog(Context context, int width, int height, View layout,
|
||||
// int style) {
|
||||
// super(context, style);
|
||||
// // 设置内容
|
||||
// setContentView(layout);
|
||||
// // 设置窗口属性
|
||||
// Window window = getWindow();
|
||||
// WindowManager.LayoutParams params = window.getAttributes();
|
||||
// // 设置宽度、高度、密度、对齐方式
|
||||
// float density = getDensity(context);
|
||||
// params.width = (int) (width * density);
|
||||
// params.height = (int) (height * density);
|
||||
// params.gravity = Gravity.CENTER;
|
||||
// window.setAttributes(params);
|
||||
//
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 获取显示密度
|
||||
// *
|
||||
// * @param context
|
||||
// * @return
|
||||
// */
|
||||
// public float getDensity(Context context) {
|
||||
// Resources res = context.getResources();
|
||||
// DisplayMetrics dm = res.getDisplayMetrics();
|
||||
// return dm.density;
|
||||
// }
|
||||
//
|
||||
// private void fullScreenImmersive(View view) {
|
||||
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||
// int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|
||||
// | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
|
||||
// | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
|
||||
// | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
|
||||
// | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|
||||
// | View.SYSTEM_UI_FLAG_FULLSCREEN;
|
||||
// view.setSystemUiVisibility(uiOptions);
|
||||
// }
|
||||
//
|
||||
// }
|
||||
|
||||
@Override
|
||||
|
||||
public void show() {
|
||||
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
|
||||
super.show();
|
||||
// fullScreenImmersive(getWindow().getDecorView());
|
||||
// this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
|
||||
}
|
||||
// @Override
|
||||
// public void show() {
|
||||
// this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
|
||||
// super.show();
|
||||
//// fullScreenImmersive(getWindow().getDecorView());
|
||||
//// this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
|
||||
// }
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user