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" />
效果:
![](http://img.blog.csdn.net/20131021153052359?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTM1MjkxOA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
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>
效果:
![](http://img.blog.csdn.net/20131021153201203?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTM1MjkxOA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
我不明白这个是怎么布设的,权重又该怎么算。有人明白的话给我指点迷津。
【*Android开发论坛-安卓开发-最活跃的android开发论坛 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/】
相关文章推荐
- Android Camera数据流分析全程记录(overlay方式)
- Android 开发权限设置中英对照说明详解
- 清单文件AndroidManifest文件可加元素
- Android服务重启
- android expandablelistview--实现类似qq界面的效果
- android 近百个源码项目
- 点击短信内容,获取短信内的号码和链接
- 基于Android的Word文档阅读器
- 使用adb命令卸载Android系统应用
- viewpager Android实现导航菜单左右滑动效果
- android底部导航栏
- Activity的四种加载模式
- Android应用代码段
- Android Activity/Service/Broadcaster三大组件之间互相调用
- Android Paint和Color类
- android启动activity的几种方式
- Android SDK下载和更新失败的解决方法
- 【android】Glut glRotatef glTranslatef示例程序:
- 【Android】Tips for Android developer: “Conversion to Dalvik format failed: Unable to execute dex: null”
- AndroidManifest.xml文件中加入文件控制控制权限