第一眼看上去是不是很复杂。其实也不是很复杂。你会觉得用Popwindow,或者使用dilaog。其实两者方法都是可以实现的,那么我就用自定义dialog的方式实现该页面。
直接上代码:
packagecom.android.view;
importandroid.app.Dialog;
importandroid.content.Context;
importandroid.content.res.Configuration;
importandroid.os.Bundle;
importandroid.support.annotation.NonNull;
importandroid.support.annotation.StyleRes;
importandroid.util.DisplayMetrics;
importandroid.util.Log;
importandroid.view.Display;
importandroid.view.Gravity;
importandroid.view.View;
importandroid.view.Window;
importandroid.view.WindowManager;
importandroid.widget.Toast;
/**
* Created by liupanpan on 2017/3/8.
*/
public classBaseDialogextendsDialog {
public static volatileBaseDialogdialoginstance;
publicContextcontext;
privateDisplaydisplay;//这个设置显示属性用的
privateViewview;
public staticBaseDialoggetInstance(Context context, intstyle) {
if(dialoginstance==null) {
synchronized(Dialog.class) {
if(dialoginstance==null) {
dialoginstance=newBaseDialog(context,style);
}
}
}
returndialoginstance;
}
public voidsetContextView(View view) {
this.view= view;
showDialog();
}
publicBaseDialog(@NonNullContext context,@StyleResintthemeResId) {
super(context,themeResId);
this.context= context;
}
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(view);
initview();
}
private voidinitview() {
WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
display= windowManager.getDefaultDisplay();
//在这里初始化 基础对话框
this.setCanceledOnTouchOutside(true);
//隐藏系统输入盘
this.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE| WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE|
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Window dialogWindow = getWindow();
WindowManager.LayoutParams Param = dialogWindow.getAttributes();
DisplayMetrics d =context.getResources().getDisplayMetrics();// 获取屏幕宽、高用
dialogWindow.setGravity(Gravity.LEFT| Gravity.TOP);
Param.width= (int) (d.widthPixels*0.9);
Param.height= (int) (d.heightPixels*0.54);// 高度设置为屏幕的0.5
Param.x=100;// 新位置X坐标
Param.y=5;// 新位置Y坐标
dialogWindow.setAttributes(Param);
}
public voidshowDialog() {
if(isShowing()) {
dismiss();
}else{
show();
}
}
public booleanisScreenChange() {
Configuration mConfiguration =context.getResources().getConfiguration();//获取设置的配置信息
intori = mConfiguration.orientation;//获取屏幕方向
if(ori == mConfiguration.ORIENTATION_LANDSCAPE) {
Log.e("Re","false");
Toast.makeText(context,"false",Toast.LENGTH_SHORT).show();
//横屏
return false;
}else if(ori == mConfiguration.ORIENTATION_PORTRAIT) {
//竖屏
Toast.makeText(context,"true",Toast.LENGTH_SHORT).show();
Log.e("Re","true");
return true;u
}
return false;
}
}
启动方式
BaseDialog.getInstance(context,R.style.webviewTheme).setContextView(view);
需要源码的 可联系我:(下方留言)
感觉好的可以打赏:
关注公众号:
qrcode_for_gh_1c53482fb215_430(1).jpg