android:layout_weight属性详解

在android开发中LinearLayout很常用,LinearLayout的内控件的android:layout_weight在某些场景显得非常重要,比如我们需要按比例显示。android并没用提供table这样的控件,虽然有TableLayout,但是它并非是我们想象中的像html里面的table那么好用,我们常用ListView实现table的效果,但是列对齐确比较麻烦,现在用LinearLayout及属性android:layout_weight能很好地解决。下面我们共同体验下layout_weight这个属性。

  一、LinearLayout内的控件的layout_width设置为"wrap_content",请看一下xml配置:

 <LinearLayout

      android:orientation="horizontal"

      android:layout_width="fill_parent"

      android:layout_height="fill_parent"

      android:layout_weight="1"

     >

      <TextView

          android:layout_width="wrap_content"

          android:layout_height="fill_parent"

          android:layout_weight="1"

          android:background="#aa0000"

          android:gravity="center"

          android:text="1"/>

      <TextView

          android:layout_width="wrap_content"

          android:layout_height="fill_parent"

          android:layout_weight="2"

          android:background="#00aa00"

          android:gravity="center"

          android:text="1"/>

      <TextView

          android:layout_width="wrap_content"

          android:layout_height="fill_parent"

          android:layout_weight="3"

          android:background="#0000aa"

          android:gravity="center"

          android:text="1"/>

  </LinearLayout>

 效果如下:

可以看到这三个TextView是按照1:2:3的比例进行显示的,这样看来似乎可以实现按照比例显示了,但是有个问题,如果TextView内的文本长度一同那么较长文本的TextView会宽度会有所增加,见下面配置及效果:

配置:

 <LinearLayout

      android:orientation="horizontal"

      android:layout_width="fill_parent"

      android:layout_height="fill_parent"

      android:layout_weight="1">

      <TextView

          android:layout_width="wrap_content"

          android:layout_height="fill_parent"

          android:layout_weight="1"

          android:background="#aa0000"

          android:gravity="center"

          android:text="1111111111111111111111111111111111111111111"/>

      <TextView

          android:layout_width="wrap_content"

          android:layout_height="fill_parent"

          android:layout_weight="2"

          android:background="#00aa00"

          android:gravity="center"

          android:text="2"/>

      <TextView

          android:layout_width="wrap_content"

          android:layout_height="fill_parent"

          android:layout_weight="3"

          android:background="#0000aa"

          android:gravity="center"

          android:text="3"/>

  </LinearLayout>

效果:

这样看来我们所需要的按比例又无法实现了,经过满天地google终于找到了解决方案,就是设置layout_width设置为"wrap_content"。配置及效果见下:

 <LinearLayout

      android:orientation="horizontal"

      android:layout_width="fill_parent"

      android:layout_height="fill_parent"

      android:layout_weight="1">

      <TextView

          android:layout_width="0dp"

          android:layout_height="fill_parent"

          android:layout_weight="1"

          android:background="#aa0000"

          android:gravity="center"

          android:text="1111111111111111111111111111111111111111111"/>

      <TextView

          android:layout_width="0dp"

          android:layout_height="fill_parent"

          android:layout_weight="2"

          android:background="#00aa00"

          android:gravity="center"

          android:text="2"/>

      <TextView

          android:layout_width="0dp"

          android:layout_height="fill_parent"

          android:layout_weight="3"

          android:background="#0000aa"

          android:gravity="center"

          android:text="3"/>

  </LinearLayout>

效果:

这样终于达到我们的按比例显示的效果了,感觉很是奇怪,android开发框架的大佬们有时候设计确实有点匪夷所思。

  二、LinearLayout内的控件的layout_width设置为"fill_parent",请看一下xml配置:

 <LinearLayout

      android:orientation="horizontal"

      android:layout_width="fill_parent"

      android:layout_height="fill_parent"

      android:layout_weight="1">

      <TextView

          android:layout_width="fill_parent"

          android:layout_height="fill_parent"

          android:layout_weight="1"

          android:background="#aa0000"

          android:gravity="center"

          android:text="1"/>

      <TextView

          android:layout_width="fill_parent"

          android:layout_height="fill_parent"

          android:layout_weight="2"

          android:background="#00aa00"

          android:gravity="center"

          android:text="2"/>

  </LinearLayout>


效果如下:

奇怪吧,整个宽度平分3块,第一个TextView占了两块,这样看来weight值越小的优先级越大。只有两个TextView似乎看出些道理,那么让我们看看三个是什么效果:

<LinearLayout

      android:orientation="horizontal"

      android:layout_width="fill_parent"

      android:layout_height="fill_parent"

      android:layout_weight="1">

      <TextView

          android:layout_width="fill_parent"

          android:layout_height="fill_parent"

          android:layout_weight="1"

          android:background="#aa0000"

          android:gravity="center"

          android:text="1"/>

      <TextView

          android:layout_width="fill_parent"

          android:layout_height="fill_parent"

          android:layout_weight="2"

          android:background="#00aa00"

          android:gravity="center"

          android:text="2"/>

      <TextView

          android:layout_width="fill_parent"

          android:layout_height="fill_parent"

          android:layout_weight="3"

          android:background="#0000aa"

          android:gravity="center"

          android:text="3"/>

  </LinearLayout>

效果:

什么意思?第三个TextView丢掉了,很是奇怪,让我们再试一个,把weight分别改为2,3,4的看看效果:

这个效果让人很困惑,我一直想寻求出一个确切的比例公式,但是至今未找到。有哪位大神能搞定的话忘不吝赐教。

虽然这个android:layout_weight属性很怪异,但幸运的是我们达到了目标:

  按比例显示LinearLayout内各个子控件,需设置android:layout_width="0dp",如果为竖直方向的设置android:layout_height="0dp"。在这种情况下某子个控件占用LinearLayout的比例为:本控件weight值 / LinearLayout内所有控件的weight值的和。

15

0

阅读 (20301)   评论  (8) 收藏 (2)  转载 (18)   喜欢   打印 举报
已投稿到:
  • yuzhishui


    计算公式为:
    linearLayout中包含有weight的child时,linearLayout会measure两次:
    设屏幕宽度为X
    第一次:textview1的measuredWidth为X,textview2,textview3也为X (因为用了weight,所以linearLayout每次measure child时不考虑前一个已经占用的大小),total_width为3X
    第二次:计算delta=x-total_width=-2x,
    然后会将
    textview1的宽度设为x+delta*1/6=2/3x, 
    textview2的宽度为 x+delta*1/3=1/3x
    textview3的宽度为 x+delta*1/3=0x (即第三个textview3不显示)

    同理:
    textview1的宽度设为x+delta*2/9=5/9x, 
    textview2的宽度为 x+delta*3/9=3/9x
    textview3的宽度为 x+delta*4/9=1/9x 

    如果需要将textview1:textview2:textview3 = 1:2:3(实际占据屏幕的比例) 
    可以这么算:1/6:2/6:3/6 --> 分母-分子-->5:4:3(即 layout_weight值)

    2012-6-21  11:19回复(0)

  • 新浪网友


    楼上厉害

    2013-7-12  17:56回复(0)

  • ximenyique


    3 当按水平排放时,如果几个控件的android:layout_width="wrap_content"
        那会先分配各个控件内部必须要占用的空间,其他剩余空间按该值得大小比例分配。
        如果几个控件的android:layout_width="fill_parent"
        会根据该值得比例反比去显示大小。
        
        如果android:layout_width

    2013-8-14  14:55回复(0)

  • Carman


    一楼正解!

    2013-12-1  10:18回复(0)

  • xsq967

    很好,学到了一招

    2014-3-4  10:06回复(0)

  • waresc


    真正的原因是Layout_width="fill_parent"的原因造成的。依照上面理解我们来分析:
    系统先给2个textview分配他们所要的宽度fill_parent,也就是说每一都是填满他的父控件,这里就死屏幕的宽度
    那么这时候的剩余空间=1个parent_width-2个parent_width=  -1个parent_width (parent_width指的是屏幕宽度 )

    2014-7-3  11:14回复(0)

  • 筱少许

    当layout_width=“fill_parent”时,如果分别给三个TextView设置他们的Layout_weight为1、2、2的话,就会出现3:1:1的比例

    你会发现1的权重小,反而分的多了,这是为什么呢???网上很多人说是当layout_width=“fill_parent”时,weighth值越小权重越大,优先级越高,就好像在背口诀
    一样,其实他们并没有真正理解这个问题,

    2014-7-15  15:34回复(0)

  • wolfbigbig

    一楼,大赞!

    2014-8-28  20:45回复(0)

  • Lawrence

    textview1的宽度设为x+delta*1/6=2/3x, 
    textview2的宽度为 x+delta*2/6=1/3x
    textview3的宽度为 x+delta*3/6=0x (即第三个textview3不显示)
    1L 这个地方是不是写错了

    2014-9-15  18:24

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目标检测(Object Detection)是计算机视觉领域的一个核心问题,其主要任务是找出图像所有感兴趣的目标(物体),并确定它们的类别和位置。以下是对目标检测的详细阐述: 一、基本概念 目标检测的任务是解决“在哪里?是什么?”的问题,即定位出图像目标的位置并识别出目标的类别。由于各类物体具有不同的外观、形状和姿态,加上成像时光照、遮挡等因素的干扰,目标检测一直是计算机视觉领域最具挑战性的任务之一。 二、核心问题 目标检测涉及以下几个核心问题: 分类问题:判断图像的目标属于哪个类别。 定位问题:确定目标在图像的具体位置。 大小问题:目标可能具有不同的大小。 形状问题:目标可能具有不同的形状。 三、算法分类 基于深度学习的目标检测算法主要分为两大类: Two-stage算法:先进行区域生成(Region Proposal),生成有可能包含待检物体的预选框(Region Proposal),再通过卷积神经网络进行样本分类。常见的Two-stage算法包括R-CNN、Fast R-CNN、Faster R-CNN等。 One-stage算法:不用生成区域提议,直接在网络提取特征来预测物体分类和位置。常见的One-stage算法包括YOLO系列(YOLOv1、YOLOv2、YOLOv3、YOLOv4、YOLOv5等)、SSD和RetinaNet等。 四、算法原理 以YOLO系列为例,YOLO将目标检测视为回归问题,将输入图像一次性划分为多个区域,直接在输出层预测边界框和类别概率。YOLO采用卷积网络来提取特征,使用全连接层来得到预测值。其网络结构通常包含多个卷积层和全连接层,通过卷积层提取图像特征,通过全连接层输出预测结果。 五、应用领域 目标检测技术已经广泛应用于各个领域,为人们的生活带来了极大的便利。以下是一些主要的应用领域: 安全监控:在商场、银行
。支持yolov5s,yolov5m,yolov5l.zip目标检测(Object Detection)是计算机视觉领域的一个核心问题,其主要任务是找出图像所有感兴趣的目标(物体),并确定它们的类别和位置。以下是对目标检测的详细阐述: 一、基本概念 目标检测的任务是解决“在哪里?是什么?”的问题,即定位出图像目标的位置并识别出目标的类别。由于各类物体具有不同的外观、形状和姿态,加上成像时光照、遮挡等因素的干扰,目标检测一直是计算机视觉领域最具挑战性的任务之一。 二、核心问题 目标检测涉及以下几个核心问题: 分类问题:判断图像的目标属于哪个类别。 定位问题:确定目标在图像的具体位置。 大小问题:目标可能具有不同的大小。 形状问题:目标可能具有不同的形状。 三、算法分类 基于深度学习的目标检测算法主要分为两大类: Two-stage算法:先进行区域生成(Region Proposal),生成有可能包含待检物体的预选框(Region Proposal),再通过卷积神经网络进行样本分类。常见的Two-stage算法包括R-CNN、Fast R-CNN、Faster R-CNN等。 One-stage算法:不用生成区域提议,直接在网络提取特征来预测物体分类和位置。常见的One-stage算法包括YOLO系列(YOLOv1、YOLOv2、YOLOv3、YOLOv4、YOLOv5等)、SSD和RetinaNet等。 四、算法原理 以YOLO系列为例,YOLO将目标检测视为回归问题,将输入图像一次性划分为多个区域,直接在输出层预测边界框和类别概率。YOLO采用卷积网络来提取特征,使用全连接层来得到预测值。其网络结构通常包含多个卷积层和全连接层,通过卷积层提取图像特征,通过全连接层输出预测结果。 五、应用领域 目标检测技术已经广泛应用于各个领域,为人们的生活带来了极大的便利。以下是一些主要的应用领域: 安全监控:在商场、银行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值