android:layout_weight深入理解

 1 :android:layout_weight 属性理解

在Linerlayout中有 android:layout_weight这个属性,是给子view分配权重,现在我们来设置三个TextView,权重分别设为1,2,2:

<?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:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:baselineAligned="false"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:gravity="center"
        android:textColor="#ffffffff"
        android:background="@color/colorPrimaryDark"
        android:text="Hello World 1 "
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="2"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:text="Hello World 2"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#ff7a61"
        android:layout_weight="2"
        android:text="Hello World 3"
        android:gravity="center"
        />
</LinearLayout>

1:2:2按比例分配,按照我们的理解应该是这样的: 图1


可是实际上却是这样的:图2


纳尼?这是怎么回事?

请看这句话: Specifies how much of the extra space in the layout to be allocated to a view.

android:layout_weigh 首先按照控件声明的尺寸进行分配,然后再将剩下的尺寸按weight分配,最终分配的大小就是 view控件宽度+剩余父控件宽度*weight

这里我们match_parent ,假设屏幕宽度500dp,那么Hello world 1 分配的宽度= 500(控件宽度) + (500-3*500)/5 = 3*500/5 就是如图2所示。占了整个屏幕的3/5(这里500-3*500 是剩余父控件宽度,他等于屏幕宽度-子view宽度之和),同理Hello world2,3 ,就分别占总屏幕的1/5:

所以正确的布局写法应该是:

<?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:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:baselineAligned="false"
    >
    <TextView
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:gravity="center"
        android:textColor="#ffffffff"
        android:background="@color/colorPrimaryDark"
        android:text="Hello World 1 "
        />
    <TextView
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="2"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:text="Hello World 2"
        />
    <TextView
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:background="#ff7a61"
        android:layout_weight="2"
        android:text="Hello World 3"
        android:gravity="center"
        />
</LinearLayout>
对宽width 分配就把width 设置0dp ,对高Hight分配就把hight设置0dp,这样就能得到图1的效果。


 2 :android:layout_weight 属性妙用

    在之前写过一篇 高仿QQ主界面,主界面是Framlayout+底部栏:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"  
    android:layout_width="match_parent" android:layout_height="match_parent"  
    android:orientation="vertical"  
   >  
  
    <FrameLayout  
        android:id="@+id/content"  
        android:layout_width="match_parent"  
        android:layout_weight="1"  
        android:layout_height="0dp">  
    </FrameLayout>  
    <include  
        layout="@layout/bottom"/>  
</LinearLayout>  

对FrameLayout 设置weight属性,对底部不设置。那么最终add到Framelayout 的Fragment将被分配除底部栏以外的所有空间,这种带底部栏的布局,以后都可以这样写,不需要用Relativelayout 布局,消耗更多的性能。

内容概要:本文围绕并网与离网模式下的风光互补制氢合成氨系统,开展容量配置与调度优化的建模与仿真研究,基于Python代码实现核心技术复现。研究聚焦于风能与太阳能发电的波动性特征,结合电解水制氢及氢气合成氨的能量转换环节,构建综合能源系统的多目标优化模型,兼顾经济性、能源利用率与系统稳定性。通过引入先进的优化算法与Cplex等求解工具,对系统关键设备容量进行优化配置,并实现多时段运行调度的精细化决策,推动可再生能源高效转化为绿色化工产品,为“电-氢-氨”一体化系统的设计与运行提供科学依据和技术支撑。; 适合人群:具备一定Python编程能力和优化建模基础,从事新能源系统、氢能利用、综合能源系统规划与运行等方向研究的科研人员、高校研究生及工程技术人员。; 使用场景及目标:①用于风光制氢合成氨系统的容量规划、运行策略制定与经济性评估;②支撑高水平学术论文的模型复现、算法验证与创新研究,提升对多能互补系统协同优化机制的理解与实践能力; 阅读建议:建议结合Cplex等优化求解器运行代码,深入理解模型构建过程中的目标函数设计与约束条件表达,重点关注可再生能源出力不确定性处理与能量转换效率建模,并参考相关文献进一步拓展优化算法与场景分析维度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值