百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程网 > 正文

LinearLayout中weigh,weightSum用法

yuyutoo 2024-10-19 11:08 20 浏览 0 评论

在Android开发中,LinearLayout是一个常用的布局,分别是horizontal(水平方向)和vertical(垂直方向)。

相信大家对其都不陌生,但我相信可能大家在学习或工作中对于weight,weightSum这个两个属性用的比较少。今天,我就用一个例子,介绍一下这个两个属性的用法。

在介绍例子前,我们先简单的介绍一下这两个属性。

1.weight属性:是将在LinearLayout中,剩余的空间按weight的比中分给自己。

假设我们有一个水平排列的LinearLayou,有两个子View,基布局参数如下:

view width weight

LinearLayout match_parent

view1 0dp 1.0

view2 0dp 1.0

那么我们view的宽度是多少呢?我们第一个View1的宽度就是:

0+ 1.0/(1.0+1.0) = 0.5*match_parent

上面的0是我们宽度,1.0是我们自己的weight,(1.0+1.0)是所有具有weight的总和。

2.sumWeight:是指总LinearLayout的总的weight,当我们指定的这个值时,我们在计算比中是用到。

假设我们有一个水平排列的LinearLayou,有两个子View,基布局参数如下:

LinearLayout提定了一个sumWeigh为5.0

view width weight

view1 0dp 1.0

view2 0dp 1.0

那么我们view的宽度是多少呢?我们第一个View1的宽度就是:

0+ 1.0/(5.0) = 0.2*match_parent

上面的0是我们宽度,1.0是我们自己的weight,5.0是sumWeigh。

下面我们给出一个例子:

xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#fff"

android:orientation="vertical">

<LinearLayout android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:background="#ff00">

<TextView android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1.0"

android:text="weigt 1.0"

android:textColor="#fff"

android:background="#cccccc"

android:gravity="center"/>

<TextView android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1.0"

android:text="weigt 1.0"

android:textColor="#fff"

android:background="#ffcccc"

android:gravity="center"/>

</LinearLayout>

<LinearLayout android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:weightSum="5.0"

android:layout_marginTop="30dp"

android:background="#ff00">

<TextView android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1.0"

android:text="weigt 1.0"

android:textColor="#fff"

android:background="#cccccc"

android:gravity="center"/>

<TextView android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1.0"

android:text="weigt 1.0"

android:textColor="#fff"

android:background="#ffcccc"

android:gravity="center"/>

</LinearLayout>

</LinearLayout>

运行后:

相关推荐

高一高二第一次月考认真作答(高二第一次月考的重要性)

正在进行高一、高二第一次月考,同学们正在认真完成化学试卷,研究考纲,探究考点,夯实基础,迎战高考!

山清水秀,盛世今朝(山清水秀出处)

万千星河,神州妖娆!山清水秀,盛世今朝!龙腾虎跃,锦绣前程!千里婵娟,祝福永远!

我校二模成绩已新鲜出炉(二模考试成绩)

充电加油备战高考,积极努力再拼一搏...

Argon Design向瑞萨电子有限公司提供Argon Streams VP9许可证

英国剑桥--(美国商业资讯)--领先的先进视频验证解决方案提供商ArgonDesignLtd已与日本半导体公司瑞萨电子有限公司(RenesasElectronicsCorporation)签署...

高考倒计时75天(高考倒计时75天励志语)

今天是2022年3月24日星期四,距离2022年高考还有75天时间对于十八岁的高三学子来说,有些事情的确会影响你们的一生,但是没有一件事能决定你们的一生!努力的意义,就是:以后的日子里,放眼望去,全...

期中考试正在进行(期中考试在即)

转眼即瞬,期中考试已到,紧张忙碌的两个月学习,检验的时刻到了。让我们拿出信心和勇气,来挑战自我。面对考验,我们该做的就是沉着,冷静。让知识来一次次洗礼我们的灵魂,让失败和成功迎接一次次的成长。你们可以...

不要浪费了你NAS上的HDMI接口!详解华硕NAS上HDMI接口的妙用

不要浪费了你NAS上的HDMI接口!详解华硕NAS上HDMI接口的妙用之前我在本站分享我使用的华硕(ASUS)AS6704T...

Java通过Kafka Streams库来实现数据流处理

#暑期创作大赛#...

From abandoned mines to limpid streams waters: how banks profit from EOD

ByZENGYanglinInthecurrentpursuitofthe“dualcarbon”target(carbonpeakingandcarbonneutra...

SPSS与Streams的集成实现实时预测

SPSSModeler是一个数据挖掘工作台,提供了一个可了解数据并生成预测模型的最先进的环境。Streams提供了一个可伸缩的高性能环境,对不断变化的数据进行实时分析,这些数据中包括传统结构的数据...

Kafka Streams, 我还会再使用它吗?

DeeptiMittal4分钟阅读...

大数据Hadoop之——Kafka Streams原理介绍与简单应用示例

一、KafkaStreams概述官网文档:https://kafka.apache.org/32/documentation/streams/...

Android上的TCP今天开始向用户推出,并将在下个月向所有用户提供

据extends网3月15日报道,Firefox今天宣布,其保护用户免受跟踪器攻击的全面cookie保护(TCP)功能现已在Android上可用。该功能默认启动模式,这样,跟踪器将无法收集有关用户的浏...

Linux curl命令(linux curl命令安装)

Linuxcurl命令是一个利用URL规则在命令行下工作的文件传输工具。它支持文件的上传和下载,所以是综合传输工具,但按传统,习惯称curl为下载工具。作为一款强力工具,curl支持包括HTTP、H...

go语言http服务入门详解(go语言http服务器)

当你在浏览器中输入URL时,实际上是在发送一个对Web页面的请求。该请求被发送到服务器。服务器的工作是获取适当的页面并将其作为响应发送回浏览器。在Web的早期,服务器通常读取服务器硬盘上HTML文件的...

取消回复欢迎 发表评论: