Android学习笔记----layout_weight属性解析
2016-03-05 19:28
369 查看
android:layout_weight属性只有在Linearlayout中起作用,而且分别设置android:layout_width为wrap_content和match_parent会造成两种截然相反的效果。
权重为1的反而占了三分之二!
更改布局如下
左边 TextView占比三分之一,又正常了。
android:layout_weight的真实含义是:一旦View设置了该属性(假设有效的情况下),那么该 View的宽度等于原有宽度(android:layout_width)加上剩余空间的占比!
设屏幕宽度为L,在两个view的宽度都为match_parent的情况下,原有宽度为L,两个的View的宽度都为L,那么剩余宽度为L-(L+L) = -L, 左边的View占比三分之一,所以总宽度是L+(-L)*1/3 = (2/3)L.
<span style="font-family:SimSun;"><LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="@color/black" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="2" android:background="@color/green" /> </LinearLayout></span>
两个TextView的宽度均设为match_parent,一个权重为1,一个权重为2.得到效果如下:
权重为1的反而占了三分之二!
更改布局如下
<strong><LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="0" <!-- 为了让宽度的比例为严格的 1:2 此处需将宽度设置为0 当然如果不是很严格的话可以是wrap_content--> android:layout_height="wrap_content" android:layout_weight="1" android:background="@color/black" /> <TextView android:layout_width="0" <!-- 为了让宽度的比例为严格的 1:2 此处需将宽度值设置为0 当然如果不是很严格的话可以是wrap_content --> android:layout_height="wrap_content" android:layout_weight="2" android:background="@color/green" /> </LinearLayout> </strong>也就是把宽度修改为wrap_content,此时会看到如下的效果
左边 TextView占比三分之一,又正常了。
android:layout_weight的真实含义是:一旦View设置了该属性(假设有效的情况下),那么该 View的宽度等于原有宽度(android:layout_width)加上剩余空间的占比!
设屏幕宽度为L,在两个view的宽度都为match_parent的情况下,原有宽度为L,两个的View的宽度都为L,那么剩余宽度为L-(L+L) = -L, 左边的View占比三分之一,所以总宽度是L+(-L)*1/3 = (2/3)L.
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories