Android Studio中的layout_weight属性讲解

Android布局权重(layout_weight)的用法解析 布局权重是Android开发中常用的一种布局属性,通过设置权重值,我们可以实现子视图在父容器中按比例分配可用空间的效果。通过权重属性,我们可以根据子视图的权重比例来计算它们的相对大小,并实现各种布局需求。Android开发中,布局权重(layout_weight)是一种常用的布局属性,用于在布局中指定子视图的相对大小比例。这是因为布局权重属性是通过动态计算子视图的大小来实现的,而非固定的像素值。布局权重属性的应用场景非常广泛,特别适用于需要在布局中实现比例分配的场景。布局权重属性的使用方式。 阅读详情

属性含义

android:layout_weight的表面含义是:分配的比重值,默认值为0 。真实含义是:一旦View设置了该属性(假设有效的情况下),那么该View的宽度等于原有宽度(android:layout_width)加上剩余空间(宽度)的占比!

控件最终宽度计算公式

控件A最终宽度 = 控件A初始宽度 +(屏幕宽度 - 控件宽度和)* 控件A的weight的值 /所有weight之和。
下面分析中所提到的剩余宽度 = 屏幕宽度 - 控件宽度和

第一种

<?xml version="1.0" encoding="utf-8"?>
    <!-- LinearLayoutCompat其实就是LinerLayout组件,只是为了兼容低版本-->
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">
    <!--第一种-->
    <Button
        android:id="@+id/button_0"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/blue"
     />

    <Button
        android:id="@+id/button_1"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/red"
       />

    <Button
        android:id="@+id/button_2"
        android:layout_weight="3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/yellow"
        />
</androidx.appcompat.widget.LinearLayoutCompat>
    <!--
    android:layout_weight的真实含义是:一旦View设置了该属性(假设有效的情况下),那么该View的宽度等于原有宽度(android:layout_width)加上剩余空间的占比!
    计算公式:
    控件A最终宽度 = 控件A初始宽度+(屏幕宽度 - 控件宽度和)* 控件A的weight的值 /所有weight之和
    下面分析中所提到的剩余宽度 = 屏幕宽度 - 控件宽度和
    -->
    <!--
        设屏幕宽度为L,三个view指定的android:layout_width都是0,所以剩余宽度为L.
        三个TextView的宽度依次为:
        0+(L)*1/6
        0+(L)*2/6
        0+(L)*3/6
    -->

第二种

<?xml version="1.0" encoding="utf-8"?>
    <!-- LinearLayoutCompat其实就是LinerLayout组件,只是为了兼容低版本-->
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">
    <!--第二种-->
    <Button
        android:id="@+id/button_0"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/blue"
        />

    <Button
        android:id="@+id/button_1"
        android:layout_weight="2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/red"
        />

    <Button
        android:id="@+id/button_2"
        android:layout_weight="3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/yellow"
        />

</androidx.appcompat.widget.LinearLayoutCompat>
    <!--
        设屏幕宽度为L,在三个view的宽度都为match_parent的情况下,原有宽度为L,三个的View的宽度都为L,剩余宽度为L-(L+L+L) = -2L
        三个TextView的宽度依次为:
        L+(-2L)*1/6 = (2/3)L
        L+(-2L)*2/6 = (1/3)L
        L+(-2L)*3/6 = (0)L
        这就是为什么layout_weight设置越大显示的占比反而越小,甚至是不显示的原因
        事实上默认的View的weight这个值为0,一旦设置了这个值,那么所在view在绘制的时候执行onMeasure两次的原因就在这。
    -->

第三种

<?xml version="1.0" encoding="utf-8"?>
    <!-- LinearLayoutCompat其实就是LinerLayout组件,只是为了兼容低版本-->
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <!--第三种-->
    <Button
        android:id="@+id/button_0"
        android:layout_weight="1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:background="@color/blue"
        />

    <Button
        android:id="@+id/button_1"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/red"
        />

    <Button
        android:id="@+id/button_2"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/yellow"
        />

</androidx.appcompat.widget.LinearLayoutCompat>

<!--
    设屏幕宽度为L,剩余宽度为L-(100dp)
    三个Button的宽度依次为:
    100+(L-(100dp))*1/3
    0+(L-(100dp))*1/3
    0+(L-(100dp))*1/3
    相当于是把L-100dp平均分为6份,然后按比重划分给每个view
    最后把100dp加到指定的view上
-->

第四种

<?xml version="1.0" encoding="utf-8"?>
    <!-- LinearLayoutCompat其实就是LinerLayout组件,只是为了兼容低版本-->
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <!--第四种-->
    <Button
        android:id="@+id/button_0"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:background="@color/black"
        />

    <Button
        android:id="@+id/button_1"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/blue"
        />

    <Button
        android:id="@+id/button_2"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/red"
        />

    <Button
        android:id="@+id/button_3"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/yellow"
        />

</androidx.appcompat.widget.LinearLayoutCompat>

<!--
    设屏幕宽度为L,剩余宽度为L-(100dp)
    三个Button的宽度依次为:
    100+(L-(100dp))*1/3
    0+(L-(100dp))*1/3
    0+(L-(100dp))*1/3
    相当于是把L-100dp平均分为6份,然后按比重划分给每个view
    最后把100dp加到指定的view上
-->

第五种

<?xml version="1.0" encoding="utf-8"?>
    <!-- LinearLayoutCompat其实就是LinerLayout组件,只是为了兼容低版本-->
<androidx.appcompat.widget.LinearLayoutCompat 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"
    tools:context=".MainActivity">

    <!--第五种-->
    <Button
        android:id="@+id/button_0"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:text="123456789123456789"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:background="@color/blue"

        />

    <Button
        android:id="@+id/button_1"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/red"
        />

    <Button
        android:id="@+id/button_2"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/yellow"
        />

</androidx.appcompat.widget.LinearLayoutCompat>

<!--
    第一个view的 android:layout_width=“wrap_content”,所以初始宽度为text的长度
    设屏幕宽度为L,剩余宽度为L-(text的长度)
    三个TextView的宽度依次为:
    text的长度+(L-(text的长度))*1/3
    0+(L-(text的长度))*1/3
    0+(L-(text的长度))*1/3
-->

第六种

<?xml version="1.0" encoding="utf-8"?>
    <!-- LinearLayoutCompat其实就是LinerLayout组件,只是为了兼容低版本-->
<androidx.appcompat.widget.LinearLayoutCompat 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"
    android:baselineAligned="false"
    tools:context=".MainActivity">

    <!--第六种-->
    <Button
        android:id="@+id/button_0"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:text="1233444556666777889"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:background="@color/blue"
        />

    <Button
        android:id="@+id/button_1"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/red"
        />

    <Button
        android:id="@+id/button_2"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/yellow"
        />

</androidx.appcompat.widget.LinearLayoutCompat>

<!--
    设屏幕宽度为L,三个view指定的android:layout_width都是0,所以剩余宽度为L.
    三个TextView的宽度依次为:
    0+(L)*1/3
    0+(L)*1/3
    0+(L)*1/3
-->

六种情况的效果图

在这里插入图片描述

理解 Android 中的布局权重(Layout Weight) 在 Android 中,布局权重(Layout Weight)是一种用于分配可用空间的属性,它允许我们在布局中设置视图的相对大小。无论使用哪种布局,布局权重的原理都是相同的,即根据权重值将可用空间分配给视图。例如,如果一个视图的权重值为2,而其他视图的权重值为1,则前者将占据后者的两倍空间。总结一下,布局权重是 Android 中一种强大的属性,它允许我们根据权重值分配可用空间,创建自适应和灵活的界面。通过上述代码,我们可以实现一个自适应的界面,无论屏幕的宽度是多少,按钮都会均匀分布并且具有相同的宽度。 阅读详情

相关推荐

11.15 分析finrl 的框架

11.15 分析finrl 的框架

yueguoqing88的博客 1088

Android:layout_weight详解

参考: http://www.cnblogs.com/zhmore/archive/2011/11/04/2236514.html http://m.blog.csdn.net/article/details?id=24667299 http://renyuan-1991.iteye.com/blog/22722001、只有在Linearlayout中,该属性才有效。 2、android:l

薛瑄的博客 5350

生信分析实战指南:Linux环境下EMBOSS的安装与核心功能详解

本文详细介绍了在Linux环境下安装和使用EMBOSS工具包的实战指南,涵盖源码编译、包管理器安装和Conda安装三种方法,并演示了序列比对、序列特征提取和引物设计等核心功能。EMBOSS作为生物信息学领域的开源工具集,适用于序列分析、蛋白质结构预测等多种任务,帮助研究人员高效完成生信分析工作。

weixin_29202213的博客 472

Android知识点剖析系列:深入了解layout_weight属性

前言   Android中layout_weight这个属性对于经常捣鼓UI的我们来说,肯定不会陌生。但是我们在真正使用这个属性时,经常会出现一些莫名奇妙的布局效果;如果仅仅知其然而不知其所以然,一些意外的布局效果一定让我们颇为头疼。在本文中,将对layout_weight这个属性详细剖析。 正文 从代码讲起: <LinearLayout xmlns:and...

abwy16733的博客 485

IEC 61850标准协议解读 3.MMS服务

摘要:本文系统介绍了MMS(制造报文规范)的基础知识及其在IEC61850中的应用。首先解析MMS的核心定义、通信流程(带确认/不带确认服务)、虚拟制造设备(VMD)概念及核心对象(域/有名变量等)。重点阐述IEC61850与MMS的映射关系,包括对象分层映射、服务转换规则及数据类型对应。深入分析ASN.1编解码机制,说明MMS PDU的分层结构与解码流程。最后通过典型报文案例(报告服务、控制服务、文件传输等)和通信初始化流程,展示MMS在智能变电站中的实际应用。全文揭示了MMS作为抽象应用层协议如何通过标

张二狗的博客 732

YOLO系列算法改进 | C3k2改进篇 | 融合MambaOut轻量化门控架构 | 纯卷积实现超越Mamba性能的同时兼顾极致轻量化与边缘部署效率 | CVPR 2025

YOLO系列算法改进 | C3k2改进篇 | 融合MambaOut轻量化门控架构 | 纯卷积实现超越Mamba性能的同时兼顾极致轻量化与边缘部署效率 | CVPR 2025 (以YOLOv26最新目标检测模型为基础,利用C3k2_MambaOut创新模块进行改进)

2502_91881744的博客 230

android layout_weight 百分比,Android百分比布局之layout_weight属性

如果转载,请注明出处:http://blog.csdn.net/wangdejun/article/details/42739983 谢谢。在Android中对控件布局指定尺寸时,一般有两种方式:一种设定为自适应布局,即match_parent(fill_parent)或者wrap_content,通过根据父布局大小或者自己内容来产生一个动态尺寸;另外一种通过指定一个具体数值的方式定义成固定...

weixin_28327051的博客 2649

SAP 消息号T-CODE

SAP消息号修改汇总 确实有很多消息是可以修改的 FI: oBA5|oFMG|oFPM MM: oMRM|oKZZ|oMT4|oMCQ|o04C| SD: oVAH(V4)|oVM1(VL) CO:  oPR4_ACT|oPR4_CK|oPR4_CKML|oPR4_CKPF|oPR4_KKA|oPR4_KKP|oPR4_KKS| oPR4_KKS1|oPR4_P

heng0757的专栏 1万+

Android studio控件了解

(以下内容是自己查资料,学习的,如有冒犯,请联系本人) 一.初学者的两种基础布局 RelativeLayout相对布局 LinearLayout线性布局 二.基础控件 layout_width: 决定布局的宽度 layout_height:决定布局的高度 layout_weight:取值一般是1、2、3…表示权重的大小(略) 把布局想成一个容器或者一个卡片,而卡片的大小就由上面两个属性决定,取值有三个方式: wrap_content:刚好把文字组件包裹的长度 match_pare

qq_46300856的博客 916

手把手教你使用YOLOv11完成人脸分类识别,YOLOv11人脸识别,解决中文乱码问题,全网最详细教程

手把手教你使用YOLOv11完成人脸分类识别,YOLOv11人脸识别,解决中文乱码问题,全网最详细教程

在职AI算法工程师,擅长计算机视觉,YOLO目标检测、分割等,擅长web、pyqt界面可视化,好内容持续更新中,来这里跟大家一起学习,共同进步 4457

【DRAM存储器二】Sense Amplifier

详述DRAM颗粒中感应放大器功能、电路结构、工作原理和操作波形

highman110的博客 7233

【Android Studio】 LinearLayout

LinearLayout线性布局

weixin_53724285的博客 2102

嵌入式Modbus协议面试题及参考答案

Modbus 协议采用主从模式进行通信,这种模式定义明确,便于在工业环境中实现设备之间的有序数据交互。在主从模式中,存在一个主站(Master)和多个从站(Slave)。主站通常是工业控制系统中的上位机,如 PLC、工控机等,它负责发起通信请求,控制整个通信过程。从站则是各种现场设备,如传感器、执行器等,它们接收主站的请求,并根据请求进行相应的操作,然后将结果返回给主站。主站的主要功能包括:发起数据读取或写入请求、控制通信的时序和流程、处理从站返回的数据。

大模型大数据攻城狮的专栏 2054

Android Studio开发 —— 界面布局

Android Studio 界面布局 Android 所有的元素都是通过 View 与 ViewGroup 构建的 ViewGroup 作为容器承载界面的控件 它可以包含 View 控件 也可以包含 ViewGroup 常用布局 RelativeLayout 相对布局 * LinearLayout 线性布局 * FrameLayout 帧布局 TableLayout 表格布局 ConstraintLayout 约束布局 根节点布局 xmlns //

LittleBridLibrary的博客 1477

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

Android入门教程 | UI布局之LinearLayout 线性布局

Android有几种布局?

Android23333的博客 538

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Android-studio

Android-studio LinearLayout 线性布局 orientation 方向 layout_weight:权重,当layout_weight设置为0dp时,他可以按比例设置元件的宽度(水平) gravity:设置文本在容器中的位置 layout_gravity:设置容器在窗口中的位置 background:设置背景色 text_color:文本颜色 layout_margin:边距 1.layout_marginLeft 在容器文本的左边填充距离

Splitss的博客 2530

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Androd布局与控件

布局

Happy__snake的博客 380

Android Studio中的布局讲解

Android studio中布局讲解

adminstate的博客 3005

Android布局之weight属性解析

Android布局之weight属性解析 分类: android学习2012-03-27 20:04 4042人阅读 评论(4) 收藏 举报 androidbuttonlayoutencodingxml 在网上看了一些对Layout_weight的讲解,有些说的比较片面,只列举了一种情况,然后自己通过实验和一些比较好的文章总结了一下,特此记录下来,以备以后所用。Layout_weight是线

calvin367的专栏 778

weightSum属性和layout_weight属性的用法和详解

在xml中有个属性我们很少用到,但它很有用,就是android:weightSum属性,下面我们讲解他的属性和用法 将按钮居中显示,并且占据其父视图宽度的一半,应该怎么做呢,肯定读者会有很多种不同的做法 如何结合LinearLayout的android:weightSum属性和LinearLayout的子视图的android: layout_weight属性来解决这个问题。这听起来

sinat_30474567的博客 1533

LinerLayout布局中android:layout_gravity = "right"失效的原因及解决方法

原因 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1+1" android:layout_gravity="right" ...

qq_43510031的博客 1465

Android Studio菜鸟开发————LinearLayout线性布局

在content_main.xml中可以这样写: xml version="1.0" encoding="utf-8"?> LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns

菜鸟程序猿的博客 1万+

Android_Studio_布局_LinearLayout(线性布局)

一、LinearLayout常用属性 1、orientation:   布局中组件的排列方式,有horizontal(水平),vertical(垂直,默认),两种方式 2、gravity:   控制组件所包含的子元素的对齐方式,可多个组合,如(left|buttom) 3、layout_gravity:   控制该组件在父容器里的对齐方式 4、layout_width:   布局的宽度,通常不直接接写数字的,用wrap_content(组件实际大小),fill_parent或者match_parent填满容

RebrithMan的学习笔记 2970

Android:关于layout_weitht的详解

在网上看了一些对Layout_weight的讲解,有些说的比较片面,只列举了一种情况,然后自己通过实验和一些比较好的文章总结了一下,特此记录下来,以备以后所用。Layout_weight是线性布局,也就是LinearLayout里面用到的,下面通过实验来看这个Layout_weight的特性。 1.当控件的属性android:layout_width="fill_parent"时,布局文件如下:

hz0324的专栏 921

Android Studio新手开发双双一天

本文介绍了Android中的线性布局(LinearLayout)及其核心属性。线性布局通过orientation属性确定子视图排列方向(水平/垂直),默认水平排列。layout_weight属性可实现子视图的权重分配,通过设置对应方向尺寸为0dp并按比例分配权重值,能实现视图的按比例布局。文中通过示例代码展示了垂直和水平方向的权重分配效果,如1:3的比例分配布局空间。

Ling_Qing_Chen的博客 432

安卓开发学习-Android Studio-04-线性布局与表格布局

一、线性布局 线性布局分为水平线性布局和垂直线性布局。 首先我们将activity_main.xml文件中的最外层标签改为< LlinearLayout>即整体为线性布局,如下图: 首先我们添加三个button,并将其layout_weight的值删除,名称进行修改。 接下来进行布局设置,添加下图中红色箭头所指示的代码对其属性android:orientation=""进行设置,它的值有两个:vertical(垂直)、horizontal(水平)。可以看到下图button的排列已经变为垂直方

qq_44091183的博客 2190
上一篇: Android Studio 下UI视图显示问题
下一篇: Android Studio中android: baselineAligned属性认识及用途
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值