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

LinearLayout中weigh,weightSum用法

yuyutoo 2024-10-19 11:08 4 浏览 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>

运行后:

相关推荐

如何在HTML中使用JavaScript:从基础到高级的全面指南!

“这里是云端源想IT,帮你...

推荐9个Github上热门的CSS开源框架

大家好,我是Echa。...

前端基础知识之“CSS是什么?”_前端css js

...

硬核!知网首篇被引过万的论文讲了啥?作者什么来头?

整理|袁小华近日,知网首篇被引量破万的中文论文及其作者备受关注。知网中心网站数据显示,截至2021年7月23日,由华南师范大学教授温忠麟等人发表在《心理学报》2004年05期上的学术论文“中介效应检验...

为什么我推荐使用JSX开发Vue3_为什么用vue不用jquery

在很长的一段时间中,Vue官方都以简单上手作为其推广的重点。这确实给Vue带来了非常大的用户量,尤其是最追求需求开发效率,往往不那么在意工程代码质量的国内中小企业中,Vue占据的份额极速增长...

【干货】一文详解html和css,前端开发需要哪些技术?
【干货】一文详解html和css,前端开发需要哪些技术?

网站开发简介...

2025-02-20 18:34 yuyutoo

分享几个css实用技巧_cssli

本篇将介绍几个css小技巧,目录如下:自定义引用标签的符号重置所有标签样式...

如何在浏览器中运行 .NET_怎么用浏览器运行代码

概述:...

前端-干货分享:更牛逼的CSS管理方法-层(CSS Layers)

使用CSS最困难的部分之一是处理CSS的权重值,它可以决定到底哪条规则会最终被应用,尤其是如果你想在Bootstrap这样的框架中覆盖其已有样式,更加显得麻烦。不过随着CSS层的引入,这一...

HTML 基础标签库_html标签基本结构
HTML 基础标签库_html标签基本结构

HTML标题HTML标题(Heading)是通过-...

2025-02-20 18:34 yuyutoo

前端css面试20道常见考题_高级前端css面试题

1.请解释一下CSS3的flexbox(弹性盒布局模型),以及适用场景?display:flex;在父元素设置,子元素受弹性盒影响,默认排成一行,如果超出一行,按比例压缩flex:1;子元素设置...

vue引入外部js文件并使用_vue3 引入外部js

要在Vue中引入外部的JavaScript文件,可以使用以下几种方法:1.使用``标签引入外部的JavaScript文件。在Vue的HTML模板中,可以直接使用``标签来引入外部的JavaScrip...

网页设计得懂css的规范_html+css网页设计

在初级的前端工作人员,刚入职的时候,可能在学习前端技术,写代码不是否那么的规范,而在工作中,命名的规范的尤为重要,它直接与你的代码质量挂钩。网上也受很多,但比较杂乱,在加上每年的命名都会发生一变化。...

Google在Chrome中引入HTML 5.1标记

虽然负责制定Web标准的WorldWideWebConsortium(W3C)尚未宣布HTML5正式推荐规格,而Google已经迁移到了HTML5.1。即将发布的Chrome38将引入H...

HTML DOM 引用( ) 对象_html中如何引用js

引用对象引用对象定义了一个同内联元素的HTML引用。标签定义短的引用。元素经常在引用的内容周围添加引号。HTML文档中的每一个标签,都会创建一个引用对象。...

取消回复欢迎 发表评论: