android:layout_weight的真实含义
2016-03-14 17:20
537 查看
首先声明只有在Linearlayout中,该属性才有效。之所以android:layout_weight会引起争议,是因为在设置该属性的同时,设置android:layout_width为wrap_content和match_parent会造成两种截然相反的效果。如下所示:
[html] view
plain copy
print?
<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="@android:color/black"
android:text="111"
android:textSize="20sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="@android:color/holo_green_light"
android:text="222"
android:textSize="20sp" />
上面的布局将两个TextView的宽度均设为match_parent,一个权重为1,一个权重为2.得到效果如下:
可以看到权重为1的反而占了三分之二!
再看如下布局:
[html] view
plain copy
print?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/black"
android:text="111"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="@android:color/holo_green_light"
android:text="222"
android:textSize="20sp" />
</LinearLayout>
即宽度为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.事实上默认的View的weight这个值为0,一旦设置了这个值,那么所在view在绘制的时候执行onMeasure两次的原因就在这。
Google官方推荐,当使用weight属性时,将width设为0dip即可,效果跟设成wrap_content是一样的。这样weight就可以理解为占比了!
原文地址:http://blog.csdn.net/yanzi1225627/article/details/24667299
[html] view
plain copy
print?
<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="@android:color/black"
android:text="111"
android:textSize="20sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="@android:color/holo_green_light"
android:text="222"
android:textSize="20sp" />
上面的布局将两个TextView的宽度均设为match_parent,一个权重为1,一个权重为2.得到效果如下:
可以看到权重为1的反而占了三分之二!
再看如下布局:
[html] view
plain copy
print?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/black"
android:text="111"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="@android:color/holo_green_light"
android:text="222"
android:textSize="20sp" />
</LinearLayout>
即宽度为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.事实上默认的View的weight这个值为0,一旦设置了这个值,那么所在view在绘制的时候执行onMeasure两次的原因就在这。
Google官方推荐,当使用weight属性时,将width设为0dip即可,效果跟设成wrap_content是一样的。这样weight就可以理解为占比了!
原文地址:http://blog.csdn.net/yanzi1225627/article/details/24667299
相关文章推荐
- Android开发者入门必知了解谷歌官方Android开发文档
- Android 之Hierarchy Tool Window
- 在Android系统外部和内部读取Android应用的签名
- Android设计模式系列(10)--SDK源码之原型模式
- android4.4短信拦截怎么实现,abortBroadcast()不能实现啊
- android GridLayout。。。
- Activity的跳转
- android中MeasureSpec的使用
- Android AutoLayout全新的适配方式 堪称适配终结者
- Android设计模式系列(9)--SDK源码之适配器模式
- Android 编程下获得应用程序的签名
- Android设计模式系列(8)--SDK源码之工厂方法模式
- android 拍照 图片剪切
- Android二维码的扫描和生成(ZXing)
- Android PRODUCT_COPY_FILE宏 使用方法
- 想要阻断短信通知必须成为android4.4默认短信应用,并且阻断两种广播才能做到
- Android控件的坑
- Android进程间通信(IPC)机制Binder简要介绍和学习plan
- ArcGIS4Android 3 :最快Android模拟器Genymotion
- Android基础-Notification用法