您的位置:首页 > 移动开发 > Android开发

android:layout_weight属性你真的理解吗

2016-04-16 21:44 363 查看
大家看到这个标题可能会说,你这也太low了吧?权重这个属性谁不知道,谁没用过,有什么好讲的?是的,我确实经常用,但是我们并非真正的理解了它。ok,下面我们说说这一属性。

权重的平分(按比例划分):

这个确实没有什么好说的。就是我们最初对他的理解。所以这里我们跳过。

剩余空间按权重平分(重点):

这就是我想要说的,为了便于我们理解这里我结合布局代码讲解:

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="35dp"
android:background="#f00"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textSize="35dp"
android:background="#0f0"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:textSize="35dp"
android:background="#00f"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="40dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="35dp"
android:background="#f00"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textSize="35dp"
android:background="#0f0"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:textSize="35dp"
android:background="#00f"/>
</LinearLayout>

<LinearLayout
android:layout_marginTop="40dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="35dp"
android:background="#f00"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textSize="35dp"
android:background="#0f0"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:textSize="35dp"
android:background="#00f"/>
</LinearLayout>
</LinearLayout>


这是布局图:



首先我们要分三类讲解:

从布局文件大家可以看到我们是按照1:2:3做的权重分配

1. android:layout_width=”0dp”这个就是初学时候我们理解的权重分配

按照1:2:3做的权重分配布局。

2.android:layout_width=”wrap_content”首先系统会给3个view分配它们的宽度值,没错就是wrap_content,然后把剩余的宽度按照1:2:3再分配给他们,即1:2:3

3.android:layout_width=”match_parent”其实前两个很好理解,主要是第三个,同样系统会给3个view分配它们的宽度值就是match_parent,

接下来我们算一算剩余的宽度:

剩余的宽度=屏幕的宽度-3match_parent

你们看是不是这样,嗯,对的,理论上就是这样。那么也可以这样写:

剩余的宽度=match_parent-3match_parent=-2match_parent

so. 剩余的宽度=-2match_parent

好的,大家看下效果图是不是第三行的最后一个TextView不见了?现在我们就可以算下,他为什么消失咯,索性我们算下他们三个的比值。

(这里解释下,下面的计算:首先每个都会现分配一个match_parent,然后在平分剩余宽度,即 match_parent+剩余的宽度*所占的比例)

第一个:

match_parent+(-2)match_parent*1/6=2/3match_parent

第二个:

match_parent+(-2)match_parent*2/6=1/3match_parent

第三个:

match_parent+(-2)match_parent*3/6=0match_parent

好了,这就是我理解的权重,如果有错误,希望大家指出。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: