`
caiwb1990
  • 浏览: 307979 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

layout-weight属性

 
阅读更多
lanyout_weight:用于给一个线性布局中的诸多视图的重要度赋值,默认为零,数值越小,重要度越高,先来看一下效果:


这里有两个Linearlayout,上面一个是三张图片,下面是几句话,这里分别给两个
Linearlayout设置lanyout_weight属性为1,2;在第一个Linearlayout中分别给三张图片设置设置lanyout_weight属性为1,2,3;

然后看另一个效果:


这里的变化只是把三张图片的lanyout_weight属性设置为1,1,3

现在再看lanyout_weight属性都为1时的布局:


三张图对比来看会给人一种错觉即lanyout_weight属性是某个组件在布局中所占的比例,
我一开始也是这么想的,但是后面发现2个问题:
1.三张图都设置为1时他们并没有平分,第三个图的比例大于前两图
2.Linearlayout本身的lanyout_weight属性和Linearlayout中组件的lanyout_weight属性的效果是反过来的(Linearlayout中lanyout_weight低的所占比例大,TextView中lanyout_weight高的所占比例大)

百度了一下有人说 “在一个LinearLayout内各个组件是按weight值以正比形式显示在屏幕上  而在LinearLayout之间以反比的形式显示在屏幕上  ”。。。

我想这个属性不会这么二吧不同组件的效果还不一样。。

最后搞到官方文档关于这个属性的解释

引用
LinearLayout  also supports assigning a weight to individual children. This attribute assigns an "importance" value to a view, and allows it to expand to fill any remaining space in the parent view. Child views can specify an integer weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero. For example, if there are three text boxes and two of them declare a weight of 1, while the other is given no weight (0), the third text box without weight will not grow and will only occupy the area required by its content. The other two will expand equally to fill the space remaining after all three boxes are measured. If the third box is then given a weight of 2 (instead of 0), then it is now declared "more important" than both the others, so it gets half the total remaining space, while the first two share the rest equally.



其实很简单,weight本来的意思就是重量,即这个属性所代表的是重要程度,而不是比例

如果三个view横列,前两个view的wieght是1,第三个是默认值0。可见第三个view的权weight最低,那么第三个view就只会占用自己内容所需的宽度,剩下的宽度会被前两个view平分。

如果第三个view的wieght是2,那么前两个view就只占据自己内容所需的宽度,第三个会占据剩下所有空间。

至于Linearlayout的weight属性越小的所占空间越多,我觉得这是因为Linearlayout的默认高度问题,一个简单的实验当我把两个Linearlayout的值设为1和5的时候,屏幕比例依然不变,因为第一个Linearlayout的默认大小就是那么大


分享到:
评论

相关推荐

    css2中文手册 学习css的必选

    font color font-family font-size font-size-adjust font-stretch font-style font-weight text-decoration text-underline-position text-shadow font-variant text-transform line-height letter-spacing word-...

    Android应用中通过Layout_weight属性用ListView实现表格

    今天主要说的是对Layout_weight属性的完全解析,以及利用Layout_weight这个属性使用ListView来实现表格的效果,我们都知道Android里面专门有一个TableLayout来实现表格的,说实话,我平常开发中用TableLayout还是...

    Android App中的多个LinearLayout嵌套布局实例解析

    查了下资料,说是要设置layout_weight属性 资料说得不是很清楚,也没仔细看,就去弄,结果越弄越混乱。 于是静下心来,自己写xml测试,发现如下。 如果LinearLayout是最外面的一层,它是不会弹出layout_weight属性的...

    LoadingLayout-一个可以控制页面4种加载状态的控件.zip

     android:layout_weight="1"  app:emptyView="@layout/empty_view"  >  你的原有的内容布局  </com.xiaomagouche.loadinglayout.library.LoadingLayout>2. 控制状态变化LoadingLayout loadingLayout = ...

    4种Android屏幕自适应解决方案

    Android支持多屏幕机制即用为当前设备屏幕提供一种合适的方式来共同管理并解析应用资源。本文就介绍了4中Android屏幕自适应解决方案。...传统的layout_weight使用方法是将当前控件的layout_width和layout_

    weightDemo

    对android布局中的layout_weight属性进行深入讲解。

    Android控件大全以及各布局空间的使用方式

    layout_centerInParent - 将当前元素放置到其容器内的水平方向和垂直方向的中央位置(类似的属性有 :layout_centerHorizontal, layout_alignParentLeft 等) layout_marginLeft - 设置当前元素相对于其容器的左侧...

    ChildLayout:处理LinearLayout,两层绘制后,无法绘制的情况

    ChildLayout描述为了解决当我们使用 LinearLayout 布局的时候使用了 weight 属性后,再嵌套LinearLayout,又一次在子 LinearLayout 中使用 weight 属性,会导致实际的 View 无法被成功绘制的解决方案,具体实际的...

    Android屏幕自适应Demo

    小编在以前开发过程中,都是通过运用Layout的weight属性来设计页面的。后来小编无意中,在网上发现了一高手的文章,年代久远,很抱歉忘记了它的地址,但是小编保留下了它的代码,现共享出来。

    LCRapidDevelop-master

    android:layout_weight="1" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:layout_marginRight="10dp...

    Android上拉面板AndroidSlidingUpPanel.zip

    如果您想定义高度屏幕为percetange,可将其设置为match_parent,滑动视图定义为layout_weight属性。  默认情况下,整个面板将作为拖动区域和将截获的点击和拖动事件。可以通过使用setDragView方法或...

    Android中Textview和图片同行显示(文字超出用省略号,图片自动靠右边)

    但是当我们要是图标跟着文字后面,第一时间想到用LinearLayout的layout_weight 来做,但是做出来效果不一样。 后来竟然忘记这个属性了:指定TextView的最大宽度实现自动省略 android:maxWidth=90dp ,加上

    android Activity线性布局和表格布局实例讲解

    其中本实验用到的各控件的属性解释如下:android:layout_weight属性是指不同的控件在activity中占有体积大小的比例。android:paddingLeft指内边距左的距离,即控件内文字离控件左边边界的距离。其它的类推。android:...

    android-CustomChoiceList-master

    Android 自定义选择列表示例 此示例演示了如何创建自定义可检查布局,以与 ListView 的 choiceMode 属性一起使用。 介绍 此示例演示了如何在 Android ... android : layout_weight = " 1 " android : paddingLeft =

    CircleIndicator-一个轻量级的viewpager指示器 ,类似于nexus5 启动器的效果.zip

     android:layout_weight="1">    android:id="@ id/viewpager_default"  android:layout_width="match_parent"  android:layout_height="match_parent"/>    android:id="@ id/indicator_default" ...

    Android应用开发中View绘制的一些优化点解析

     用 layout_weight属性的LinearLayout 将会导致大量的开销,因为每个子视图都需要被测量两次。这是反复解析  布局文件时重要的一点,例如在ListView或者GridView中使用时。  观察你的布局    

Global site tag (gtag.js) - Google Analytics