展会信息港展会大全

android Layout_weight详解
来源:互联网   发布日期:2015-10-14 07:43:50   浏览:1085次  

导读:以前一直没弄懂Layout_weight是什么意思,自己写代码测试也出来了不同的情况,最近看了一篇帖子感觉分析的很好,转贴出来学习下。布局文件是:<?xml version="1.0" e...

以前一直没弄懂Layout_weight是什么意思,自己写代码测试也出来了不同的情况,最近看了一篇帖子感觉分析的很好,转贴出来学习下。

布局文件是:

<?xml version="1.0" encoding="utf-8"?>

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

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<Button

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="Button1"

/>

<Button

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="2"

android:text="Button2"

/>

</LinearLayout>

出现的布局是:button1占了2/3,button2占了1/3。

但是如果将布局文件中的button的属性android:layout_width="fill_parent"改为android:layout_width="wrap_content"那么出现的结果为:button1占了1/3,button2占了2/3。

出现这样的结局是什么意思呢?下面是人家的详解:转载过来:

*******转载的解释*********

linearLayout中包含有weight的child时,linearLayout会measure两次:

设屏幕宽度为X

第一次:button1的measuredWidth为X,button2也为X (因为用了weight,所以linearLayout每次measure child时不考虑前一个已经占用的大小),total_width为2X

第二次:计算delta=x-total_width=-x,然后会将button1的宽度设为

x+delta*1/3=0.66x, button2的宽度为 x+delta*2/3=0.33x

那我现在对这句话重新概括一下:“因为设置了button1的权重最小,所以它占用的布局优先级就越高”,也许在Android里面布局并没有优先级之说,我这里只是为了说明问题,自己定义的,所以朋友们不要拍砖。

那首先分析一下

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港