Android中的layout_weight属性

来源:互联网 发布:数据库视频下载 编辑:程序博客网 时间:2024/06/16 06:48

前言

  之前用到layout_weight属性,对于为什么要把layout_width or layou_height 设置为0dp不得其解。今天偶然看到一篇文章说了这一点,所以在此记录一下, 同时借助博客告诉更多人,如若侵权,请立即联系我!我马上删除。

介绍

  layout_weight,顾名思义,是重量、权重的意思,代表这个控件的权重或者说在空间中所占的重要性有多大。它用于对父布局剩余空间的进行划分,比如控件1的权重为2,控件2的权重为1,则理论上控件1占剩余空间的3分之2,而控件2占剩余空间的3分之1。举例(注意这里我们先按标准写法来):

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="2"        />    <Button        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1"        /></LinearLayout>

这里写图片描述
  根据上图看,确实我们的按钮控件分割了父布局width的空间,且为3:2,那我们改为wrap_content呢,会是什么效果呢?修改代码为:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="2"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"        /></LinearLayout>

这里写图片描述
  我们会发现没变啊,这不是可以用wrap_content吗?注意wrap_content的作用为:The view should be enough big to enclose its content。就是说设置这个属性的控件所占的空间必须足够大以包含它所有具有的content,这里因为我们的content为空,所以可以看做layout_width = 0,效果与上等同。但如若你的控件有content,将不再是这个样子,代码如下(为了对比效果,这里我让两个控件的权重比为1:1):

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="55555"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="666666666666666666666666666"        /></LinearLayout>

这里写图片描述
  这里可以看到两个控件竟然不是按照1比1来划分空间,难道不应该是两个控件各占一半吗?layout_weight失效了吗?其实并没有失效,只是我们要修正上面的定义:它用于对父布局剩余空间的进行划分,修正为应用于对父布局剩余空闲空间的进行划分。所以上面那个图对应的是父布局的长度=button1的content长度+2分之1剩余空闲空间+button2的content长度+2分之1剩余空闲空间。
  也就是说:2分之1剩余空闲空间 = (父布局长度 - button1的content长度 - button2的content长度)/ 2;因为我们设置了wrap_content,所以在分配时,会优先为控件分配这些空间,之后再把剩余空间按权重再分配。如何切掉button1和button2的初始所占空间呢,很明显我们只要设置layout_width=0来告诉android我这个控件无需预留空间,请直接按权重分配。试验一下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="55555"        />    <Button        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="666666666666666666666666666"        /></LinearLayout>

这里写图片描述
  果然,当我们设置预留空间为0时,就可以按照真正的权重来划分了,可以看到因为按钮2的内容太多了,横向无法发展,只好纵向发展了(因为你设置了wrap_content,若固定一个高度,则无法纵向发展,只能丢弃后面的内容)。
  
  注意:layout_weight是不用在relativelayout中,想想就知道,相对布局是规定内含控件之间的相对位置,可以类比网页的浮动效果,它把控件从文档流删除了,本身不占空间(这样表述不太正确)。

原创粉丝点击