您的位置:首页 > 移动开发 > Android开发

Android开发之layout_weight属性的应用

2013-10-21 15:51 387 查看
在Android开发过程中,Android开发者之间的交流学习是互通开源的。关于Android开发中layout布局文件中常用到的属性,有很多小盆友不了解,今天eoeandroid开发论坛(http://www.eoeandroid.com/就来为童鞋们整理分享如下内容:

在layout布局文件中经常用到android:layout_width、android:layout_width及android:layout_weight这些属性。那么android:layout_weight代表什么呢?不要译为体重,重量。Weight也可表示比重、权重,这在测量学中比较常见。

默认值为0,即视图大小与屏幕空间大小相等,屏幕有多大,视图就有多大。如果将其设为大于0的数值,则将父视图的可用空间分割,分割大小具体取决于每个视图的layout_weight的值,该值在当前屏幕布局的整体 layout_weight值和在其它视图屏幕布局的layout_weight值中所占的比率而定。

打个比方,水平布局有两个图片,一个图片值为1,另一个图片值也为1,则两者平分视图;或者一个图片值为1,另一个图片值为2,则第一个图片所占视图为2/3,第二张图片所占视图为1/3。

1、代码:

< ImageView

android:id="@+id/imageView1"

android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:tint="#33ff9966"

android:src="@drawable/c" />

<ImageView

android:id="@+id/imageView2"

android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:src="@drawable/c" />

效果:



2、代码:在布局文件中

<LinearLayout xmlns: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="redwwwwwww"

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="2"/>

<TextView

android:text="blue"

android:gravity="center_horizontal"

android:background="#0000aa"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="3"/>

<TextView

android:text="yellow"

android:gravity="center_horizontal"

android:background="#aaaa00"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="4"/>

</LinearLayout>

</LinearLayout>

效果:



我不明白这个是怎么布设的,权重又该怎么算。有人明白的话给我指点迷津。

【*Android开发论坛-安卓开发-最活跃的android开发论坛 http://www.eoeandroid.com/】
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: