layout界面元素记录

转发的百度博文!做为学习用

Android的layout_weight属性释疑

2011-05-13 11:26

进行断点测试的两种设置方法:

android:debuggable="true" 在工程中进行断点调试。

<uses-library android:name="android.test.runner" /> <instrumentation

android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.sihuatech.shanxi.ui" android:label="Tests for My App" />

1、界面的根标签

LinearLayout:里面可以放多个控件,但是一行只能放一个控件

RelativeLayout:里面可以放多个控件,不过控件的位置都是相对位置 

AbsoluteLayout:里面可以放多个控件,并且可以自己定义控件的x,y的位置 

TableLayout:这个要和TableRow配合使用,很像html里面的table

FrameLayout:里面只可以有一个控件,并且不能设计这个控件的位置,控件会放到左上角 

GridView:里面没有控件,九宫格界面时使用 

2、界面标签属性介绍

android:id="@+id/itemImage" 定义控件名 

android:text="text" 指定控件内容

android:layout_width="fill_parent" 设置控件的宽度

 android:layout_height="fill_parent" 设置控件的高度

android:layout_toRightOf="@+id/itemImage" 设置控件的位置在指定控件的右侧 android:layout_toLeftOf="@+id/itemImage" 设置控件的位置在指定控件的左侧 android:layout_below="@+id/itemImage" 设置控件的位置在指定控件的下侧 

android:layout_marginTop 上偏移的值 

android:layout_marginBottom 下偏移的值 

android:layout_marginLeft 左偏移的值 

android:layout_marginRight 右偏移的值

android:layout_centerHorizontal 如果为true,将该控件的置于水平居中 

android:layout_centerVertical 如果为true,将该控件的置于垂直居中 

android:layout_centerInParent 如果为true,将该控件的置于父控件的中央

android:layout_alignParentTop 如果为true,将该控件的顶部与其父控件的顶部对齐 android:layout_alignParentBottom 如果为true,将该控件的底部与其父控件的底部对齐 android:layout_alignParentLeft 如果为true,将该控件的左部与其父控件的左部对齐 android:layout_alignParentRight 如果为true,将该控件的右部与其父控件的右部对齐 

android:layout_above 将该控件的底部置于给定ID的控件之上

android:layout_alignBaseline 将该控件的baseline与给定ID的baseline对齐 

android:layout_alignTop 将该控件的顶部边缘与给定ID的顶部边缘对齐 

android:layout_alignBottom 将该控件的底部边缘与给定ID的底部边缘对齐 

android:layout_alignLeft 将该控件的左边缘与给定ID的左边缘对齐 

android:layout_alignRight 将该控件的右边缘与给定ID的右边缘对齐

 android:gravity="center" 设置控件的文字居中

layout_weight是LinearLayout布局里一个重要的属性,就像Qt里的stretch一样,把父视图剩余的空间分配给设置了layout_weight的组件。这个属性可以让LinearLayout里不同的组件分配不同宽度/高度变得非常灵活。Android官网里对 layout_weight如下解释:

LinearLayout also supports assigning a weight to individual children. This attribute assigns an "importance" value to a view, and allows it to expand to fill any remaining space in the parent view. Child views can specify an integer weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero. For example, if there are three text boxes and two of them declare a weight of 1, while the other is given no weight (0), the third text box without weight will not grow and will only occupy the area required by its content. The other two will expand equally to fill the space remaining after all three boxes are measured. If the third box is then given a weight of 2 (instead of 0), then it is now declared "more important" than both the others, so it gets half the total remaining space, while the first two share the rest equally.

大意就是layout_weight的值越大,所占比例也越大。没错,这跟我们平常理解的stretch

是一致的,但是如果把它理解成 layout_weight值相同则组件占用宽度或高度一致的话就错了,

这里说的是布局完了之后的剩余空间如何分配,layout_weight相同只说明剩余空间的分配大小

相同,而组件的实际宽度/高度则是组件需要的空间加上layout_weight分配的空间。layout_weight

的设置和 layout_height,layout_width不同的组合得出来不同的结果!

所有的view的layout_weight缺省值都是为0,意味着他们只在屏幕上占据它们需要显示的空间大小。注意:这个属性针对的是父控件剩余的空间,如果不配置则它的默认最小显示该控件,若有两个控件,一个的值为1,另一个值为2则父控件的空间2/3给设置为2的控件。属性是设置控件占据的空间比例的。

且先看官网上关于LinearLayout的例子: 

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

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">

<LinearLayout

android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <TextView

android:text="red"

android:gravity="center_horizontal"

android:background="#aa0000" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> <TextView

android:text="green"

android:gravity="center_horizontal" android:background="#00aa00" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> <TextView

android:text="blue"

android:gravity="center_horizontal" android:background="#0000aa" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> <TextView

android:text="yellow"

android:gravity="center_horizontal" android:background="#aaaa00" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> </LinearLayout> <LinearLayout

android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <TextView

android:text="row one" android:textSize="15pt"

android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView

android:text="row two" android:textSize="15pt"

android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/>

<TextView

android:text="row three" android:textSize="15pt"

android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView

android:text="row four" android:textSize="15pt"

android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> </LinearLayout> </LinearLayout>

在2.2的模拟器上显示的结果如下:

各颜色条的宽度是不一致的,因为red,green,blue和yellow这几个字符串的长度不一样,如果将几个标签都改成test,则得到:

现在四个颜色条的宽度是相同的了,如果想既保持字符串不一致,又想得到相同宽度的颜色条: 把TextView中的属性android:layout_width值改为"fill_parent" 得到的结果似乎可以满足要求:再这个基础上red颜色条的layout_weight设置为2。

红色颜色条彻底没了,对此不知道如何解释。如果layout_width是wrap_content的话则是正常的:

第7/11页

注意,这里几个颜色条的占用比例并不是2:1:1:1,原因之前说了。 利用嵌套的LinearLayout可以达到2:1:1:1的显示效果 <LinearLayout android:orientation="horizontal"

android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1">

<LinearLayout android:orientation="horizontal"

android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="3">

<TextView android:text="red" android:gravity="center_horizontal" android:background="#aa0000" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>

<LinearLayout android:orientation="horizontal"

android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="2">

<LinearLayout android:orientation="horizontal"

android:layout_width="fill_parent" android:layout_height="fill_parent"

第8/11页

android:layout_weight="1">

<TextView android:text="green" android:gravity="center_horizontal" android:background="#00aa00" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>

<LinearLayout android:orientation="horizontal"

android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1">

<TextView android:text="blue" android:gravity="center_horizontal" android:background="#0000aa" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>

<LinearLayout android:orientation="horizontal"

android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1">

<TextView android:text="yellow" android:gravity="center_horizontal" android:background="#aaaa00" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout> </LinearLayout> </LinearLayout>

第9/11页

再测试LinearLayout之间的layout_weight设置带来的影响,将第一个LinearLayout的layout_weight设置为2之后:

第10/11页

和view里的效果刚好相反,layout_weight大的LinearLayout占得比例最小,刚好成反比。不知道Google为什么要采用这样截然相反的策略。

最后总结一下吧:

1. LinearLayout内部的子控件之间的layout_weight是按照正比例分配空间 2. LinearLayout之间的layout_weight是按照反比例分配空间

3. 在Horizontal的LinearLayout中,控件A和控件B的layout_weight分别设置为2和1,并不代表两者的宽度之比为2:1。控件的宽度等于空间本身需要的宽度,加上通过layout_weight设置分配到了父空间里的宽度。垂直方向的LinearLayout也同理。

4.要想实现控件A和控件B的宽度严格按比例显示,可以每个控件之上都添加一个LinearLayout,在LinearLayout的属性里设置layout_weight.

转载于:https://my.oschina.net/u/936871/blog/131623

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值