Android 父控件wrap_content随笔
2016-03-28 17:25
501 查看
<pre name="code" class="html"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout_parent" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/holo_green_light" android:orientation="horizontal" > <ImageView android:id="@+id/img1" android:layout_width="64dp" android:layout_height="64dp" android:src="@drawable/ic_launcher" /> <ImageView android:id="@+id/img2" android:layout_width="164dp" android:layout_height="164dp" android:background="@android:color/holo_orange_light" android:src="@drawable/ic_launcher" /> <LinearLayout android:id="@+id/layout1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_purple" android:orientation="vertical" > </LinearLayout> </LinearLayout>
如上 先上结论 id为 layout_parent 的父控件里的wrap_content 是根据子控件的height 来计算的,计算的优先级是如果子控件中设置了固定高度,则计算 "固定[u]高度",如 64dp 。计算固定高度时会根据最大的高度来计算父控件的高度。如果子控件中没有固定高度,则根据子控件的高度来计算父控件的高度。[/u]
如上边的代码,父控件中的height 为 wrap_content,即根据内容设置高度。而它的子控件中img1,img2都为 固定数值 ,即有明确数字,则根据较大的数值,即img2的164dp得到父控件(layout_parent)的高度为164dp。 子控件layout1的高度为match_parent 即填充父类,则layout1的高度也为164dp
计算父控件大小的优先级:(父控件设置为wrap_content的前提下)
子控件设置固定大小的情况下:则根据 “设置最大固定大小的子控件的大小” 来计算父控件的大小,不计算match_parent,fill_parent;
子控件未设置固定大小的情况下:则根据 子控件的大小计算父控件的大小,如子控件为match_parnet,则父控件大大小即为填充屏幕
随笔
相关文章推荐
- Android View绘制13问13答
- Android最佳性能实践(四)——布局优化技巧
- 谈谈Android 6.0运行时权限理解
- Android中Activity和Service简单交互(通过广播机制)
- 浅析android应用增量升级
- android开发--drawerlayout(v4)
- Android中动态设置布局高度一致
- TextView ImageSpan 用法
- layout_gravity和gravity的区别
- android替换当前应用的ClassLoder两种方式
- AndroidAnnotations开发框架使用详解
- 《50 Android Hacks》学习心得二:自定义ViewGroup
- Android Spinner点击选中Item不再调用onItemSelected方法的方案
- android install soft
- 30天入门 Android 开发, Google 与你一起圆梦
- Android 内存泄漏案例和解析
- Android 中的 Service 全面总结
- android include 标签多次调用内部控件
- Android 开发进阶指南
- Android自定义控件(二)