layout_weight及常见属性解析

我们看一下下面的代码

<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="horizontal" >
    <TextView android:layout_width="0dp" android:layout_height="40dp" android:layout_weight="1" android:background="#f00" android:text="111111111111" android:gravity="center" />
     <TextView android:layout_width="0dp" android:layout_height="40dp" android:layout_weight="2" android:background="#0f0" android:gravity="center" android:text="2" />
      <TextView android:layout_width="0dp" android:layout_height="40dp" android:layout_weight="3" android:background="#00f" android:gravity="center" android:text="3" />
</LinearLayout>

看到上面的代码 我们预期的效果是什么样子的 下面让一下我们的效果
layout_weight及常见属性解析_第1张图片

我觉得这个不是我们希望的结果 我们期望的是这样的
layout_weight及常见属性解析_第2张图片

那么上面的代码怎样才能实现这样的效果呢 只需要在linelayout标签中加上一个属性
android:baselineAligned=”false”

下面我们将第一个空间的宽度设置为自适应

<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="horizontal" >
    <TextView android:layout_width="wrap_content" android:layout_height="40dp" android:layout_weight="1" android:background="#f00" android:text="111111111111" android:gravity="center" />
     <TextView android:layout_width="0dp" android:layout_height="40dp" android:layout_weight="2" android:background="#0f0" android:gravity="center" android:text="2" />
      <TextView android:layout_width="0dp" android:layout_height="40dp" android:layout_weight="3" android:background="#00f" android:gravity="center" android:text="3" />
</LinearLayout>

我们再看一下效果
layout_weight及常见属性解析_第3张图片

这样我们可以总结一下
Linelayout中的layout_weight属性首先按照控件声明的尺寸进行分配然后再将剩下的尺寸按 weight分配

下面我们再实现第三个例子 也就是将所以控件的宽度设置为充满
比例我们也改一下

<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:baselineAligned="false" android:orientation="horizontal" >
    <TextView android:layout_width="match_parent" android:layout_height="40dp" android:layout_weight="1" android:background="#f00" android:text="111111111111" android:gravity="center" />
     <TextView android:layout_width="match_parent" android:layout_height="40dp" android:layout_weight="2" android:background="#0f0" android:gravity="center" android:text="2" />
      <TextView android:layout_width="match_parent" android:layout_height="40dp" android:layout_weight="2" android:background="#00f" android:gravity="center" android:text="3" />
</LinearLayout>

看看我们的效果图
layout_weight及常见属性解析_第4张图片

可以看到我们权重为1的比权重为2的所占的 位置都宽 那么这个是因为什么呢 就需要我们上面得出的结论
首先偶们假设整个屏幕的宽度是480dp
480-480*3=-480*2;
textview1 : 480-480*2*1/5=480*3/5
textview2.3: 480-480*2*2/5=480*1/5

由上面的算数我们可以看出第一个textview占3份 第二个第三个textview占一份

我们总结:控件的宽度+父控件剩余宽度*比例

第四个例子 我们的textview占据一行的一半

<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:baselineAligned="false" android:weightSum="2" android:orientation="horizontal" >
    <TextView android:layout_width="0dp" android:layout_height="40dp" android:layout_weight="1" android:background="#f00" android:text="111111111111" android:gravity="center" />

</LinearLayout>

我们可以像上面这样实现 总的权重设置为2 但是我们的textview的权重设置为1 就是下面这样的效果
layout_weight及常见属性解析_第5张图片

带layout的属性都是布局的属性 不带的都是自身的属性

你可能感兴趣的:(layout_weight及常见属性解析)