原地址:http://blog.csdn.net/u012833380/article/details/49885721
因为我也算是初学吧,遇到很多问题也会懵。总是百度来百度去的,但是这次确实是没找到,虽然是个比较低级的问题,相信找不到的应该不止我一个。所以也算是为了提醒自己,也算是帮助别人。所以就给大家记录了下来。
因为我想要实现一个点击弹出的用户协议dialog。所以还要限制它的高度和宽度。这里我给限制了高度为设备高度的2/3,宽度为设备宽度的3/4。
这是有问题的那个页面。
customDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//记住这句话要在setContentView和窗体宽高设置之前执行,否则会报requestFeature() must be called before adding content错误。
public static MyCustomDialog createDiaLog(Context context) {
spreferences = context.getSharedPreferences(Config.SHARE_PRE_FILE, 0);
customDialog = new MyCustomDialog(context);
View view = LayoutInflater.from(context).inflate(R.layout.dialog_user_deal, null, false);
customDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//这句话,就是决定上面的那个黑框,也就是dialog的title。
int width = spreferences.getInt("screenWidth", 480);
int height = spreferences.getInt("screenHeight", 800);
Window window = customDialog.getWindow();//这部分是设置dialog宽高的,宽高是我从sharedpreferences里面获取到的。之前程序启动的时候有获取
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams lp = window.getAttributes();
lp.width = width / 4 * 3;
lp.height = height / 3 * 2;
window.setAttributes(lp);
customDialog.setContentView(view);
customDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
return customDialog;
}
这是解决之后的效果,额,内容只是我复制360的,因为公司那边也没有给内容,所以为了看效果,只能复制一下了。