展会信息港展会大全

Android:layout_weight详解
来源:互联网   发布日期:2015-10-13 12:39:15   浏览:2692次  

导读:中文定义:在一个布局组件中(例如:LinearLayout)中各个子元素的对于剩余空间的使用权重。默认:android:layout_weight= 0默认情况下,布局模式以android:height 和 android:weight为标准案例:现在设置2个......

中文定义:

在一个布局组件中(例如:LinearLayout)中各个子元素的对于剩余空间的使用权重。

默认:android:layout_weight= 0

默认情况下,布局模式以android:height 和 android:weight为标准

案例:

现在设置2个LinearLayout 个占据一张,第一行有左右各一个按钮,中间设置一个文本显示框

第二行设置一个按钮,主要是为了使第一行的文本显示框自动扩张大小,布局文件如下:

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

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

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

<Button

android:id="@+id/btn1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/com_frank_android_app_cal_zero" />

<TextView

android:id="@+id/btn2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/cal_btn_numeric_shape"

android:text="@string/com_frank_android_app_cal_one" />

<Button

android:id="@+id/btn3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/com_frank_android_app_cal_two" />

</LinearLayout>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

<Button

android:id="@+id/btn4"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/com_frank_android_app_first_btnShow" />

</LinearLayout>

</LinearLayout>

测试一:所有组件的宽度全部设置成 【warp_content】 包裹可显示的内容,未设置任何weight属性

结果非常简单,如下图:

改变文本显示框的大小以后,我们可以发现2号按钮无法显示了,这个事因为Android LinearLayout不同于Swing FlowLayout,无法显示的控件不会换行显示,见下图:

假设我们现在希望,0号按钮和1号按钮在文本显示框内容变化时候保持位置不变,可以利用weight来实施设置,请动手在TextView的配置文件中增加android:layout_weight = 1,这样我们将会看到下图的显示结果:

案例解释(重点):

Weight并非网上很多文章所叙述的那样(文章都过于片面),weight是指某个组件在布局中【剩余空间】中的显示权重,那么所谓的剩余空间又是什么意思呢,其实是在没有设置weight属性的控件优先显示的情况,将未占用的布局空间合理分配给设置过weight的组件

在上例中,0号和2号按钮都设置了android:width= warp_content",因此文本显示框只能显示在2个按钮中的【剩余空间】,不管宽度如何变动都不能超过这个宽度。

测试二:所有组件的宽度全部设置成 【warp_content】 包裹可显示的内容,0号按钮设置weight=1、文本显示框设置weight=2, 2号按钮不设置weight,显示结果如下图:

案例解释(重点):

首先我们可以看到,2号按钮没有设置过weight被优先显示为wrap_content了,并且占据了右侧位置(为何会占据右侧,请看下面的组件加载策略),所有剩余空间被依次分配给了0号按钮和文本显示框

由于文本显示框的权重更高(2)所以占据了剩余空间的2/3 而0号按钮只能使用1/3

注意:在文本框内容变动时候,0号按钮和2号按钮都能正常显示。因为0号按钮和文本显示框只能在剩余空间内活动,无法干涉2号按钮的显示位置。

根据测试推导出的Activity加载组建的策略:

一、主流程

开始-> 加载Activity->进入Activity生命周期->onCreate->加载layout配置文件->

加载容器组建(ViewGroup)->加载各个组件

二、组建加载流程

各组件加载完毕 -> 加载无weight属性组件的高度和宽度 >按weight和剩余空间换算有weight属性的组件的宽度或者高度(根据父容器的orientation属性决定)-> 加载他们的宽度或者高度->显示

提示:在版本较高的ADT中,设置了weight属性后,ADT会给出一次提示警告建议将拥有weight属性组件的高度和宽度设置成0dp,因为这个组件的宽度已经不是由配置文件能决定的了

读者可以自行按以上方法测试宽度等于match_parent的情况,结果将会与以上推论完全一致。

实际应用案例:

设计一个带有折叠列表的Dialog窗口,效果如下:

折叠打开后的效果:

赞助本站

人工智能实验室

相关热词: layoutweight

AiLab云推荐
推荐内容
展开

热门栏目HotCates

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