//权重和父容器orientation有关

horizontal 指水平方向权重  android:layout_width

vertical  指垂直方向权重   android:layout_height

Layout_weight是线性布局,也就是LinearLayout里面用到的,下面通过实验来看这个Layout_weight的特性。
1.当控件的属性android:layout_width="fill_parent"时,布局文件如下:
Xml代码 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
    android:orientation="horizontal" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <Button android:layout_width="fill_parent" 
        android:layout_height="wrap_content" android:layout_weight="1" 
        android:text="Button1" /> 
    <Button android:layout_width="fill_parent" 
        android:layout_height="wrap_content" android:layout_weight="2" 
        android:text="Button2" /> 
</LinearLayout> 
 在这里Button1的Layout_weight=1,Buttong2的Layout_weight=2,运行效果为:


我们看到,Button1占了2/3,Button2占了1/3。如果此时把button2的weight设置成2000,不是说Button2就消失了,而是Button1的宽度几乎占满了屏幕宽度,而屏幕最后一丝细条则是留给Button2的,已近非常小了,没有显示出来。截图如下:

得出结论:在layout_width设置为fill_parent的时候,layout_weight代表的是你的控件要优先尽可能的大,但尽可能大是有限度的,即fill_parent.
 
2.当控件的属性android:layout_width="wrap_content"时,布局文件如下:
 
Xml代码 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
    android:orientation="horizontal" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <Button android:layout_width="wrap_content" 
        android:layout_height="wrap_content" android:layout_weight="1" 
        android:text="Button1" /> 
    <Button android:layout_width="wrap_content" 
        android:layout_height="wrap_content" android:layout_weight="2" 
        android:text="Button2" /> 
</LinearLayout> 
 同样,Button1的weight设置为1,Button2的weight设置为2,但是效果与fill_parent的效果截然相反。运行效果如下:


这时,和fill_parent正好相反,Button1的宽度占据了屏幕宽度的1/3,而Button2的宽度占据了屏幕的2/3,如果此时把Button1的weight设置为2000,按照之前理解,Button1应该小的几乎在屏幕上看不到,但是错了,实验告诉我们,当Button1的weight非常小时,也要"wrap_content",也就是要保证Button1至少能够显示。以下是Button1设置weight为2000时的运行截图:


我们看到,Button1已经足够小,但是要保证他能显示出来,因此得出结论:
在layout_width设置为wrap_content的时候,layout_weight代表的是你的控件要优先尽可能的小,但这个小是有限度的,即wrap_content.
当了解这些后,我们再设计程序时,为了能够自适应屏幕,不想给控件一个指定的宽度和高度,就可以使用这个weight属性来让它按自己比例来划分屏幕高度或者宽度了

android 布局权重问题(转载)的更多相关文章

  1. android 布局权重问题(最近布局经常坑爹)

    android 布局 权重 With layout_weight you can specify a size ratio between multiple views. E.g. you have ...

  2. Android开发之线性布局详解(布局权重)

    布局权重 线性布局支持给个别的子视图设定权重,通过android:layout_weight属性.就一个视图在屏幕上占多大的空间而言,这个属性给其设 定了一个重要的值.一个大的权重值,允许它扩大到填充 ...

  3. Android布局管理详解(1)—— LinearLayout 线性布局

    Android的布局方式共有6种,分别是LinearLayout(线性布局).TableLayout(表格布局).FrameLayout(帧布局).RelativeLayout(相对布局).GridL ...

  4. Android布局整理Relative/Linear

    1.RelativeLayout布局 android:layout_centerHorizontal 水平居中 android:layout_centerVertical 垂直居中 android:l ...

  5. Android布局详解之一:FrameLayout

      原创文章,如有转载,请注明出处:http://blog.csdn.net/yihui823/article/details/6702273 FrameLayout是最简单的布局了.所有放在布局里的 ...

  6. android 布局如何支持多种不同屏幕尺寸

    android 布局如何支持多种不同屏幕尺寸 --关于dp.layout-xxx.drawable-xxx作用的小结 转载自:http://blog.csdn.net/vincent_blog/art ...

  7. Android 布局学习之——Layout(布局)具体解释二(常见布局和布局參数)

     [Android布局学习系列]   1.Android 布局学习之--Layout(布局)具体解释一   2.Android 布局学习之--Layout(布局)具体解释二(常见布局和布局參数)   ...

  8. Android布局及属性归总(查询用)

    常见布局 LinearLayout    线性布局        子元素任意,组织成一个单一的水平或垂直行,默认为水平方向TableLayout    表格布局        子元素为<Tabl ...

  9. [置顶] xamarin android 布局尺寸了解

    为了使UI界面在不同大小的移动端显示器上能够正常显示,大家可能都知道使用sp作为字体大小的单位,dp作为其他元素长度的单位. 前几天看了一篇文章关于 App设计规范的,文章用心写的非常好,这里是链接  ...

随机推荐

  1. C++ Code_combobox

    主题 1. 代码设置组合框风格 2. 调整组合框列表部分大小 3. 代码设置组合框相关属性 4. 自绘组合框 5. 用代码让combobox的的列表弹出 6. 不添加重复项目           代码 ...

  2. Android两个控件叠在一起,如何让被挡住的控件显示出来

    Android两个控件叠在一起,如何让被挡住的控件显示出来 问题 : 两个控件叠在一起,如何让被挡住的控件显示出来? 比如A,B两个控件,A被B挡住,目前A要显示出来,B不能被隐藏,A的高度只有那么一 ...

  3. thickbox关闭子页后ajax局部刷新父页

    1. 首先注意需要调用thickbox的js <script type="text/javascript" src="<%=path%>/js/jque ...

  4. Hdu 2473(并查集删除操作) Junk-Mail Filter

    有木有非常吊 加强 加强版   啊  ,看了都不敢做了   .后来先做了食物链这个我还是看过的.但还是A不掉,没明确神魔意思 .总而言之.大牛的博客是个好东西.我就那么看了一下,还是不懂怎莫办啊,哎, ...

  5. <转载>解决div里面img的缝隙问题

    转载自:http://blog.sina.com.cn/s/blog_9fd5b6df01013mld.html   练习切图时发现img和父级div之间总是有2px空隙(chrome),上网搜索解决 ...

  6. 20151224jquery学习笔记---cookie插件

    hello,祝自己平安夜快乐. Cookie 是网站用来在客户端保存识别用户的一种小文件.一般来用库可以保存用户登录信息.购物数据信息等一系列微小信息.一. 使用 cookie 插件官方网站: htt ...

  7. JS实现rgb与16进制颜色相互转换

    1.rgb转16进制 function to16 (a) {//RGB(204,204,024) //十六进制颜色值的正则表达式 var reg = /^#([0-9a-fA-f]{3}|[0-9a- ...

  8. ORACLE 数据库用户备份及表备份

      表备份模式备份:exp system/pwd@127.0.0.1:1521/db owner=(user) file=E:\DB\db20150326.dmp  tables=(table);还原 ...

  9. oc for in 的时候nsscanner: nil string argument

    今天偶然发现,oc for in 动态的给一数组加东西,然后嵌套for in 会报nsscanner: nil string argument. 换成for循环就好了,暂时还没找到原因

  10. Implicit conversion from enumeration type 'enum CGImageAlphaInfo' to different enumeration type 'CGB

    Implicit conversion from enumeration type 'enum CGImageAlphaInfo' to different enumeration type 'CGB ...