LinearLayout中的layout_weight属性

LinearLayout中的layout_weight属性

 

 

布局LinearLayout代码

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >  
<Button
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:text="Button1"
      android:background="#008000"
      android:layout_weight="0"
/>
<Button
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:text="Button2"
      android:background="#FFFF00"
      android:layout_weight="0"
/>
</LinearLayout>

 

layout_weight属性如果没有设置,则默认为0.

 

 

按钮1:button1,绿色

按钮2:button2,黄色

 

下面以按钮1和按钮2的weight属性(layout_weight)的变化,分别进行记录。

 

 

这里测试的是垂直布局android:orientation="vertical",所以针对组件的 android:layout_height属性;如果测试的水平布局,则需针对组件的 android:layout_width属性。

 

 

(一) android:layout_height = "fill_parent"

 

 


  button1(weight=0) button2(weight=0)   button1(weight=0) button2(weight=1)


 


 

button1(weight=1) button2(weight=1)

button1占1/2,button2占1/2

button1(weight=1) button2(weight=2)

button1占2/3,button2占1/3



 


 

button1(weight=1) button2(weight=9)

button1占9/10,button2占1/10

button1(weight=1) button2(weight=30)

button1占30/31,button2占1/31

 

 

(二) android:layout_height = "wrap_content"

 



 


 

button1(weight=0) button2(weight=0)

button1(weight=0) button2(weight=1)


 


 

button1(weight=1) button2(weight=1)

button1占1/2,button2占1/2

button1(weight=1) button2(weight=2)

button1占1/3,button2占2/3



 


 

button1(weight=1) button2(weight=9)

 

button1(weight=1) button2(weight=30)

 

 

通过以上实验可知:

(一) android:layout_height = "fill_parent"

(二) android:layout_height = "wrap_content"

在(一)、(二)2种情况在相同的weight属性下呈现出正好相反的情况。

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值