Android修改AlertDialog宽和高以及设置AlertDialog的背景
2017-04-12 22:26
645 查看
不知道你们试过了吗,AlertDialog在我们给他设置我们自己的布局的时候他的宽度不论我们怎么设置他都是不变的,要想改变宽和高我们可以动态的去修改他的宽度好高度
直接上代码
上面代码通过AlertDialog.Builder的setView()方法设置我们自己的布局
通过builder.create()获取AlertDialog对象,然后通过AlertDialog的etWindow().setLayout(DensityUtil.dip2px(context,300), LinearLayout.LayoutParams.WRAP_CONTENT);设置AlertDialog的宽和高
DensityUtil.dip2px是dp转px的工具类
在创建AlertDialog.Builder对象的时候我们有传入两个参数一个是Context另一个就是AlertDialog的样式 注意这个样式使我们自己定义的
在这里我贴出我自己定义的样式的代码
路径res/values/styles
直接上代码
// 1. 布局文件转换为View对象 LayoutInflater inflater = LayoutInflater.from(context); RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.mydialog_layout, null); // 2. 新建对话框对象 final AlertDialog.Builder builder = new AlertDialog.Builder(context,R.style.AlertDialog); //通过setView设置我们自己的布局 builder.setView(layout); final AlertDialog dialog =builder.create(); dialog.show(); //此处设置位置窗体大小 dialog.getWindow().se 4000 tLayout(DensityUtil.dip2px(context,300), LinearLayout.LayoutParams.WRAP_CONTENT);
上面代码通过AlertDialog.Builder的setView()方法设置我们自己的布局
通过builder.create()获取AlertDialog对象,然后通过AlertDialog的etWindow().setLayout(DensityUtil.dip2px(context,300), LinearLayout.LayoutParams.WRAP_CONTENT);设置AlertDialog的宽和高
DensityUtil.dip2px是dp转px的工具类
在创建AlertDialog.Builder对象的时候我们有传入两个参数一个是Context另一个就是AlertDialog的样式 注意这个样式使我们自己定义的
在这里我贴出我自己定义的样式的代码
路径res/values/styles
<style name="AlertDialog" parent="@android:style/Theme.DeviceDefault.Dialog"> <!--该行代码就是设置AlertDialog的背景--> <item name="android:background">@drawable/dialog_bg</item> </style>
相关文章推荐
- Android Dialog设置透明背景以及位置
- android下spinner值的修改以及alertDialog值的获取
- Android AlertDialog以及Dialog设置视图时区别
- android dialog 半透明效果,以及显示位置设置
- Android 设置dialog背景全透明无边框
- android 自定义Dialog背景透明及显示位置设置
- android 自定义Dialog背景透明及显示位置设置
- android 自定义Dialog背景透明及显示位置设置的方法
- 自定义Dialog,去除系统默认黑色背景以及边框并设置dialog的显示位置
- android 自定义Dialog背景透明及显示位置设置
- android 4.2修改设置菜单的背景颜色
- 获取android 手机屏幕的大小以及动态设置背景图片的两种方法;
- android AlertDialog设置点击button不退出
- android源码解析--AlertDialog及AlertDialog.Builder
- android AlertDialog透明度的设置
- android 自定义Dialog背景透明及显示位置设置
- android 自定义Dialog背景透明及显示位置设置
- android 自定义Dialog背景透明及显示位置设置(转载)
- android 自定义Dialog背景透明及显示位置设置
- android 自定义Dialog背景透明及显示位置设置