您的位置:首页 > 其它

设置Dialog全屏显示

2016-01-27 14:26 369 查看
默认的Dialog是不能全屏的。也就是怎么设置Dialog的Layout和通过

Window window = mDialog.getWindow(); 

WindowManager.LayoutParams layoutParams = window.getAttributes();

layoutParams.width = (int) (screenUtils.getWidth());

layoutParams.height = (int) (screenUtils.getHeight());

都没用的。

 

给出实现Dialog实现全屏的两种方式:

1、代码实现。这中方法相对比较简单

首先继承Dialig,然后再构造函数中添加

  super(context, android.R.style.Theme);

  setOwnerActivity((Activity)context);

这里面显示Dialog是将状态栏也隐藏掉了,如果不想隐藏就指定长宽,可以采用设置layoutParams.长宽属性,然后通过

WindowManager.LayoutParams  layoutParams = window.getAttributes();

这里面的layoutParams 去作为新的参数传给windows

即:window.setAttributes(layoutParams);

2、XML实现

首先,在values文件中添加一个XML文件,

其次,在XML文件中设置一个style

然后,添加如下代码:

    <style name="Dialog_Fullscreen"> 

       <item name="android:windowFullscreen">true</item> 

       <item name="android:windowNoTitle">true</item>  

    </style>  

最后,在代码里设置Dialog的Theme

    Dialog dialog = new Dialog(this, R.style.Dialog_Fullscreen);  

    dialog.setContentView(R.layout.main);  

    dialog.show();

里面的ScreenUtils我也贴出来

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;

/**
* 获取屏幕的分辨率帮助类
* @param context
* @return width or height
* @see ScreenUtils
* */
public class ScreenUtils {
private int width, height;
private Context context;

public ScreenUtils(Context context) {
this.context = context;
init();
}

private void init() {
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
width = outMetrics.widthPixels;
height = outMetrics.heightPixels;
}

public int getWidth() {
return width;
}

public int getHeight() {
return height;
}
}


【欢迎上码】
【微信公众号搜索 h2o2s2】

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Dialog全屏