android LayoutInflater加载的布局大小不受控制
来源:互联网 发布:如何关闭蜂窝移动数据 编辑:程序博客网 时间:2024/04/29 23:57
在使用LayoutInflater 加载布局时,经常会碰到这样的情况,明明在布局里设置了布局的大小,为什么就是不起作用,但是同样的布局 在setContentView() 中就可以起作用呢!在查看底层LayoutInflater 中的inflate 方法中终于找到了答案!
首先 我们来看 inflate(intresource, ViewGroup root, booleanattachToRoot) 方法中其他两个参数的含义:
1. 如果root为null,attachToRoot将失去作用,设置任何值都没有意义。
2. 如果root不为null,attachToRoot设为true,则会给加载的布局文件的指定一个父布局,即root。
3. 如果root不为null,attachToRoot设为false,则会将布局文件最外层的所有layout属性进行设置,当该view被添加到父view当中时,这些layout属性会自动生效。
4. 在不设置attachToRoot参数的情况下,如果root不为null,attachToRoot参数默认为true。
而我们在调用 inflate(intresource, ViewGroup root) 方法时,起始就是在调用以上的方法
/** * Inflate a new view hierarchy from the specified xml resource. Throws * {@link InflateException} if there is an error. * * @param resource ID for an XML layout resource to load (e.g., * <code>R.layout.main_page</code>) * @param root Optional view to be the parent of the generated hierarchy. * @return The root View of the inflated hierarchy. If root was supplied, * this is the root View; otherwise it is the root of the inflated * XML file. */ public View inflate(int resource, ViewGroup root) { return inflate(resource, root, root != null); }而 layout_width和layout_height 是一个布局的属性,它的含义表示“我在布局中的宽度或者高度”,但是如果仅仅使用layoutInflater加载布局时,最外层的布局文件是没有父布局了,所以它的layout_width和layout_height属性将不起作用了!这样的话我们的解决办法就很简单了,只要在布局文件中添加父布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:layout_width="300dp" android:layout_height="80dp" android:text="Button" > </Button> </RelativeLayout>这样我们的布局文件属性就可以起作用了,不管是设置到dialog,还是popuwindow 都可以呈现了!
第二:为什么同样的在setContentView 中就是起作用的,其实在setContentView 中已经默认添加了一个父布局FrameLayout,所以它可以很好的呈现效果
0 0
- android LayoutInflater加载的布局大小不受控制
- 自定义Toas工具类t(二),LayoutInflater加载的布局大小不受控制的解决办法
- Android布局加载之LayoutInflater
- android一个BaseAdapter的使用(LayoutInflater加载自定义布局)
- Android--使用LayoutInflater加载布局文件的三种方法
- Android LayoutInflater(布局加载器)详解
- LayoutInflater的使用:动态加载布局
- LayoutInflater加载布局
- layoutInflater动态加载布局
- LayoutInflater 动态加载布局
- LayoutInflater如何加载布局?
- Android布局 使用LayoutInflater动态加载布局和操作控件
- Android使用LayoutInflater动态加载布局和操作控件
- android中加载第二个布局之LayoutInflater
- Android之LayoutInflater加载布局及原理分析
- Android 中LayoutInflater(布局加载器)之介绍篇
- Android 中LayoutInflater(布局加载器)之源码篇
- Android 中LayoutInflater(布局加载器)之实战篇
- 自定义六角形头像图片控件
- 从Dynamics CRM2011到Dynamics CRM2016的升级之路
- [课本划重点]深入理解jvm-第3章 垃圾收集器与内存分配策略(1)
- logback logback.xml常用配置详解(二)<appender>
- 动态设置Ext.form.FormPanel控件里子控件的显示和隐藏
- android LayoutInflater加载的布局大小不受控制
- LightOj 1370欧拉函数快速筛法
- Activity启动流程
- Apache Http Sever的安装与使用
- SQL多表连接查询
- iOS 获取字符串中的单个字符
- HTML5 和 HTML4的区别
- jee、spring、spring mvc、mybatis 学习(七)
- 使用 ViewPager + GridView 导致页面卡顿