当前位置: 首页 > ds >正文

layout布局_关于 layout_weight,你到底知多少

  • 什么是 layout_weight

layout_weight 是 Android 线性布局中的权重表示方式, 用来表示子布局所占父布局的比重。

若 C-child 表示子布局声明的大小,B-blank 表示剩余布局的大小,P-percent 表示子布局占据父布局剩余布局的比例,则子布局最终的实际大小 R-reality 为:

R = C + B * P

通过以上公式,下面的布局依然成立: left 所占宽度:R = 0 + B * (1/2) = (1/2) * B

<LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="@color/orange_ff9800"android:padding="6dp"android:text="left"android:textColor="@color/white"/><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="@color/blue_31b6e7"android:padding="6dp"android:text="right"android:textColor="@color/white"/></LinearLayout>

3b4d82b0382ce9f335a4cc9de6e1dd45.png
  • weightSum

LinearLayout 有个权重数量的标记:weightSum,以上布局中两个 TextView 分别声明了自己的权重,在 LinearLayout 没有声明 weightSum 时,默认的权重数量就是子布局权重之和:2。

如果将以上布局的 Linearlayou 权重之和声明为 4:

<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:weightSum="4"android:background="@color/gray_e0e0e0"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="@color/orange_ff9800"android:padding="6dp"android:text="left"android:textColor="@color/white"/><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="@color/blue_31b6e7"android:padding="6dp"android:text="right"android:textColor="@color/white"/></LinearLayout>

则子布局的显示如下:

3387389fceeb5081ce672d481135b479.png
  • 0dp 与 wrap_content

谷歌官方建议子布局的 layout_width 使用 0dp,来分比例显示布局,和 wrap_content 大同小异,当使用 layout_weight 时,都表示占据剩余宽度或高度的比重。

但两者有明显区别。

使用 0dp 时,要考虑所分配的布局宽度是否小于控件实际宽度,比如:

<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:background="@color/gray_e0e0e0"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="@color/orange_ff9800"android:padding="6dp"android:text="left"android:textColor="@color/white"/><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="19"android:background="@color/blue_31b6e7"android:padding="6dp"android:text="right"android:textColor="@color/white"/></LinearLayout>

bfe3cfb2b41b5d2b534c1677671537cf.png

而使用 wrap_content 不必考虑这样的问题:

<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:background="@color/gray_e0e0e0"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:background="@color/orange_ff9800"android:padding="6dp"android:text="left"android:textColor="@color/white"/><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="19"android:background="@color/blue_31b6e7"android:padding="6dp"android:text="right"android:textColor="@color/white"/></LinearLayout>

a92bf37ebba922ecd8e1931181f14808.png
  • wrap_content 与 match_parent

当子布局的高宽声明为 match_parent 时,所显示的效果与 wrap_content 截然相反。

<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:background="@color/gray_e0e0e0"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:background="@color/orange_ff9800"android:padding="6dp"android:text="left"android:textColor="@color/white"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="2"android:background="@color/blue_31b6e7"android:padding="6dp"android:text="right"android:textColor="@color/white"/></LinearLayout>

933d1e273d664e1f219d2e1f8da9a5b7.png

究其原因,都是 match_parent 在捣鬼。

来看下文章开始时的公式: R = C + B * P

以 left 控件为例,此时的 B(剩余布局) = 父布局大小 - 子控件大小之和,父布局大小也就是 C(因为声明的是 match_parent),而子控件大小之和是 2C,所以 :

B = C - 2C = -C , R = C + B * P = C + (-C) * (1/3) = (2/3) * C

其实,以上公式恒成立,关键在于,当子控件的高宽声明为 match_parent 时的剩余布局大小要搞清楚。剩余布局大小 = 父布局大小 - 子布局大小之和

知我者乎​www.zhihu.com
d7c18b94cf0b1b5dd92ff3ba8c25f676.png
http://www.xdnf.cn/news/11289.html

相关文章:

  • 硅基流动完成近亿元融资:加速生成式AI技术普惠进程
  • 想要自己制作一款游戏,需要掌握哪些基本技能
  • android HorizontalScrollView讲解
  • 3D动画软件
  • 电脑操作系统的安装
  • RapidXML的使用
  • Qt 是一个功能强大的跨平台开发框架
  • match_parent和wrap_content的区别
  • C++语法基础--ostream,cout及其格式控制,缓冲区
  • electron一键生成项目图标
  • 抖音短视频常用的scheme跳转协议地址,直播间Intent跳转
  • 华为“天才少年”有多牛?入职不到一年,算法就用于千万台华为手机
  • 黑客入门教程(非常详细)从零基础入门到精通,看完这一篇就够了
  • WIN2016群集存储
  • IIS是什么
  • “大数据中台建设方案详解“
  • crashdump(crashdumps文件夹可以删除吗)
  • HTML如何制作公司网站首页(web前端期末大作业)
  • 妙趣解释127.0.0.1到底是什么
  • EndNote20下载安装教程(非常详细)从零基础入门到精通,看完这一篇就够了(3)
  • 宏基4750网卡驱动linux,宏基4750g网卡驱动下载
  • dell 灵越N5110 拆机
  • 解密Qphone:腾讯很现实 聚焦非智能手机市场
  • 易景地球三维平台介绍
  • Hydra(九头蛇)工具使用
  • 针对Sodinokibi黑客组织供应链攻击Kaseya VSA的分析溯源
  • 与Apollo共创生态:Apollo7周年大会自动驾驶生态利剑出鞘
  • 人立方
  • 800BUY收购鲜花网站莎啦啦 张毅图谋来年上市
  • 《复仇者联盟2:奥创纪元》720高清迅雷下载