Android控件 -- 设置组件所占比例

1、当我们使用Linearlayout线性布局,放置三个TextView控件,设置android:layout_width属性为wrap_content,并分别设置android:layout_weight比重为1,2,3时:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent" >
 
     <TextView
         android:id="@+id/textView1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:background="#990033"
         android:text="1" />
 
     <TextView
         android:id="@+id/textView2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="2"
         android:background="#339933"
         android:text="2" />
 
     <TextView
         android:id="@+id/textView3"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="3"
         android:background="#0000CC"
         android:text="3" />

</LinearLayout>

可以看到三个TextView所占的宽度比为1:2:3,似乎是根据我们所设置的android:layout_width比重实现了比例控制,然而,当我们试着把textview1的文本内容修改为111111111时:


原有的比例平衡被打破了,现在三个TextView的宽度比不再是1:2:3。

为什么呢?因为layout_width属性并不对整个可用空间进行分配,而是对剩余空间进行分配,也就是说系统会先按layout_width设置的属性先给控件分配空间,在这里的代码里是先分配空间使得每个控件足以包裹住内容,再将剩余的内容按layout_weight所设置的比例进行分配,控件最终的空间大小是layout_width属性设置的空间大小加上按比例分得的剩余空间大小,你细心观察将可以发现,上图UI中除去内容之外的控件还是1:2:3。

2、我们将layout_width的属性改为0dp试试:


这时出现了两行 可使用 android:maxLine="1"。

3、我们再来看一个更好玩的,将layout_width设置为match_parent属性,再将三个控件比例设置为1:2:2。

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" >
  
      <TextView
          android:id="@+id/textView1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_weight="1"
         android:background="#990033"
         android:text="1" />
 
     <TextView
         android:id="@+id/textView2"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_weight="2"
         android:background="#339933"
         android:text="2" />
 
     <TextView
         android:id="@+id/textView3"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_weight="2"
         android:background="#0000CC"
         android:text="2" />
 
 </LinearLayout>


不是说好按比例控制的吗?为什么TextView1所占的比例为1,确比占比为2的控件获得的空间更大的呢?

  我们来用刚刚的公式好好算算:

  剩余空间,是用父控件的大小也就是1个parent减去layout_width所设置的属性大小,每个控件原本的大小都设置为match_parent,也就是每个控件原本就应该获得1个parent控件的大小,这里有3个textview控件,也就是剩余空间的大小=1个parent-3个parent大小=-2个parent,于是textview1的大小=原来的大小1个parent+分配的剩余空间大小就是(-2parent)*1/5,最后等于3/5个parent,而其余的两个控件可以算出是1/5个parent,所以比例是3:1:1。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值