Android学习之LinearLayout比例布局计算

LinearLayout比例布局计算

LinearLayout布局关键的三个属性是:

  • 1、layout_width

  • 2、layout_height

  • 3、layout_weight

宽高一般采用wrap_content或match_parent或者fill_parent

看看下面xml布局

<?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"
    android:orientation="horizontal">


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#98FB98"
        android:text="one" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        android:background="#FFFF00"
        android:text="two" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="3"
        android:background="#FF00FF"
        android:text="three" />
</LinearLayout>

在这里插入图片描述
上面宽度采取的是wrap_content的方式,最后呈现的视图比例是1:2:3
如果把上面的layout_width换成match_parent呢,最后的比例还是1:2:3吗?可以运行一下,结果的比例是2:1:0,在网上看到一个计算公式,此种情况下

布局比例 = 父容器空间大小 + 权重比例 * 剩余空间大小
可以参考以下计算过程进行理解
1、3个都是match_parent,但是屏幕只有一个,剩余空间大小是 1 -3 = -2
2、第一个 1 + 1/6*(1-3) = 2/3
3、第二个 1 + 2/6*(1-3) = 1/3
4、第三个 1 + 3/6 * (1-3) = 0
结果是 2 :1 : 0

当比例是1:1:1时如下

1、3个都是match_parent,但是屏幕只有一个,剩余空间大小是 1 -3 = -2
2、第一个 1 + 1/3*(1-3) = 1/3
3、第二个 1 + 1/3*(1-3) = 1/3
4、第三个 1 + 1/3 * (1-3) = 1/3
结果是 1 :1 : 1
在这里插入图片描述

发布了23 篇原创文章 · 获赞 0 · 访问量 804

猜你喜欢

转载自blog.csdn.net/qq_43581283/article/details/104499483
今日推荐