控制Dialog的窗口大小(自定义Dialog视图)
来源:互联网 发布:怎么做网络平台 编辑:程序博客网 时间:2024/05/22 19:13
LayoutInflater的坑
LayoutInflater加载布局的时候,会将XML文件中的根View控件大小等属性去除,从而造成在根View中设置视图在大小无效。而Dialog中的父容器默认大小是自动的,其大小于子View大小决定,所以,LayoutInflater加载XML生成的View在Dialog中显示的效果很多是很不理想的。
例子:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <EditText android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="搜索内容" /> <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="搜索" /></LinearLayout>
以上这段代码,Dialog的视图大小应该是100DP左右,刚好能完整显示Button。
**
需求:控件Dialog显示大小为300dp
错误的布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="300dp" android:layout_height="wrap_content" android:orientation="vertical" > <EditText android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="搜索内容" /> <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="搜索" /></LinearLayout>
因为LayoutInflater加载XML的时候会将根View的属性去掉,所以在根View中设置的300dp宽度最终会被去掉,造成Dialog的最大大小为刚好能完整显示Button的大小,大约为100dp吧。
正确的布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <EditText android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="搜索内容" /> <Button android:id="@+id/btn" android:layout_width="300dp" android:layout_height="wrap_content" android:text="搜索" /></LinearLayout>
Button因为不是根View,所以它的大小在LayoutInflater中不会被去除,会完整被加载。根View的大小因为Button子View的大小为300dp而设置自动设置为300dp,所以Dialog的大小也自动设置为300dp了。
自由控件Dialog视图
在XML布局非根View设置想要的大小即可,参照以上例子。
1 0
- 控制Dialog的窗口大小(自定义Dialog视图)
- 控制自定义Dialog的大小和位置
- Android自定义Dialog大小控制
- android自定义Dialog大小控制
- 设置Dialog窗口的大小
- 设置窗口大小(包括设置Dialog的窗口)
- 控制自定义Dialog的大小和位置(计算器对话框的制作)
- Dialog大小控制和Dialog中使用的ListView自适应
- Android_Dialog_设置Dialog窗口的大小
- easyUI的dialog父窗口大小
- 自定义dialog视图
- 自定义Dialog设置大小
- 自定义Dialog位置,大小
- Dialog自定义大小
- 自定义Dialog,并设定Dialog的大小以及位置
- Dialog 大小和位置的控制
- [MFC]Dialog窗口大小调整控制OnSize/OnSizing
- DialogFragment自定义dialog的位置和大小
- centos7下lamp快速搭建环境
- qt 问题
- HDU 1506【单调栈】
- tomcat session管理
- Spring事务管理(基础概念)
- 控制Dialog的窗口大小(自定义Dialog视图)
- Oracle数据库sql--单表查询
- 用eclipse上传项目到github上
- IplImage 截取感兴趣的部分 ROI函数--只显示截取部分
- 大龄青年转行IT的真实感受1
- 通过手机访问电脑上Apache的端口
- Jquery单击选择权限实例
- linux perf - 性能测试和优化工具
- 常见前端面试题及答案(下)