layout_weight体验(实现按比例显示)

注:LinearLayout中的TextView按比例显示的时候,layout_width="0dp"或者layout_height="0dp"

 

在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值的和。

注:原文链接http://www.cnblogs.com/zhmore/archive/2011/11/04/2236514.html

  • 0
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
### 回答1: `layout_weight` 是在 Android 中用于布局的一个属性,它用于指定一个视图在布局中所占用的权重。布局中每个视图都有一个默认的权重为 `0`,当设置了 `layout_weight` 属性后,系统会根据视图的权重和所在容器的剩余空间来计算视图的实际大小。一个视图的实际大小会先根据自身的宽度或高度计算出来,然后再根据它的权重和其他视图的权重来分配剩余的空间。因此,通过设置 `layout_weight` 属性,可以实现在布局中更灵活的视图排列和分配。 ### 回答2: layout_weightAndroid布局中的一个属性,用于控制视图在父布局中的相对权重。 在LinearLayout中,父布局必须为水平或垂直方向,子视图的layout_weight属性决定它们在父布局中所占的比例。例如,如果有两个子视图,一个layout_weight为1,另一个为2,那么第一个子视图将占据父布局的1/3,而第二个子视图将占据2/3。 在RelativeLayout中,layout_weight属性无效,这是因为RelativeLayout是根据视图之间的关系来布局的。不过,可以结合使用其他属性来达到类似的效果。 layout_weight属性常用于构建灵活的布局,例如等分屏幕的两个或多个部分,或者按比例调整视图的大小。通过合理设置layout_weight,可以实现适应不同屏幕尺寸和方向的布局。 需要注意的是,更高的layout_weight并不一定意味着更大的视图,而是表示相对权重的增加。要实现适应不同分辨率的布局,还需要考虑使用dp和match_parent等属性。 综上所述,layout_weight是一种用于控制视图在父布局中相对权重的属性,可以帮助实现灵活的布局。了解并熟练使用该属性,可以更好地适配不同屏幕和实现更好的用户界面体验。 ### 回答3: layout_weightandroid中用于布局控制的一个属性。该属性只能在LinearLayout中使用。 layout_weight用于控制View在父容器中的分配空间比例。在LinearLayout中,如果某个方向的width或height设置为0dp(match_parent也可以),并且设置了layout_weight属性,那么这个View将根据layout_weight的值来分配父容器中剩余的空间。 假设有一个水平方向的LinearLayout,其中包含了两个子View:View1和View2。如果View1的layout_weight设置为1,View2的layout_weight设置为2,那么View1将会占据1/3的水平空间,而View2将占据2/3的水平空间。 另外,如果在设置了layout_weight的View中,将width或height设置为0dp,它将会以所占比例的形式拉伸或压缩来填充剩余空间。例如,如果View1的layout_weight设置为1,width设置为0dp,而View2的layout_weight设置为2,width设置为wrap_content,那么View1将会占据1/3的水平空间,并且会被拉伸或压缩以填充剩余的空间,而View2将会根据内容自动调整宽度。 总结来说,layout_weight属性允许我们在LinearLayout中根据所设置的权重值来分配剩余的空间,从而实现不同比例的布局效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值