【转】android gravity属性 和 weight属性
有点忘记这两个属性了,复习一下。
来看这个布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="one"/>
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="two"/>
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="three"/>
- </LinearLayout>
这是一个很正常的布局文件了,效果如下。

当我们给这三个EditText设置上gravity属性之后效果如下,先看 xml文件。
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="left"
- android:text="one"/>
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:text="two"/>
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="right"
- android:text="three"/>
- </LinearLayout>

由此可以看出gravity属性是用来控制 EditText里边文本的位置。
我们现在对xml文件做如下修改
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="left"
- android:text="one"/>
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:layout_weight="1.0"
- android:text="two"/>
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="right"
- android:text="three"/>
- </LinearLayout>
运行效果如下

感觉上这个android:layout_weight="1.0"是用来设置控件的大小,因为经过我们的设置中间的那个EditText变大了。其它两个EditText的 android:layout_weight属性我们没有设置,没有设置就会使用默认值, 默认值为 0.0。 设置为1.0的那个控件会填充剩余空白的部分。
我们如果想要3个组件均等地共享空间,应该将他们的weight属性都设置为 1.0,这样将均匀的扩展每一个EditText。

android:gravity是用来设置控件里面的子控件等的对齐方式的
android:layout_gravity是用来设置控件本身在它的父控件中的对齐方式!!!!
from:http://byandby.iteye.com/blog/1020374
【转】android gravity属性 和 weight属性的更多相关文章
- android gravity属性 和 weight属性
来看这个布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:and ...
- Android开发技巧一--weight属性实现视图的居中(半)显示
面试时,一位面试官问到:“如果我想讲按钮居中显示,并且占据其父视图宽度的一半,应该怎么做到呢?”即实现这种效果: 我们使用weightSum属性和layout_weight属性实现这一要求: < ...
- android: android 布局中的weight 属性
android: weight是线性布局的特有属性,控件的宽度和高度的不同,也会存在差异. 示例1:将宽度设置为包裹类型wrap_content或0dp <?xml version=" ...
- Android组件---四大布局的属性详解
[声明] 欢迎转载,但请保留文章原始出处→_→ 文章来源:http://www.cnblogs.com/smyhvae/p/4372222.html Android常见布局有下面几种: LinearL ...
- Android Hack1 使用weight属性实现视图的居中显示
本文地址:http://www.cnblogs.com/wuyudong/p/5898403.html,转载请注明源地址. 如果要实现如下图所示的将按钮居中显示,并且占据父视图的一半,无论屏幕是否旋转 ...
- Android之使用weight属性实现控件的按比例分配空间
从今天開始,把看书时候的知识点整理成博客, 这个比較简单,预计有经验的都用过,weight属性 在做Android布局的时候,常常遇到须要几个控件按比例分配空间的情况 比方下图效果 在底部设置两个bu ...
- Android weight属性详解
android:layout_weight是一个经常会用到的属性,它只在LinearLayout中生效,下面我们就来看一下: 当我们把组件宽度设置都为”match_parent”时: <Butt ...
- android:layout_gravity和android:gravity属性的区别(转)
gravity的中文意思就是”重心“,就是表示view横向和纵向的停靠位置 android:gravity:是对view控件本身来说的,是用来设置view本身的文本应该显示在view的什么位置,默认值 ...
- android:layout_gravity和android:gravity属性的区别
一.介绍: gravity的中文意思就是”重心“,就是表示view横向和纵向的停靠位置 (1).android:gravity:是对view控件本身来说的,是用来设置view本身的内容应该显示在vie ...
随机推荐
- C++ opencv 滑动条 Trackbary以及处理三通道和单通道图像
#include <opencv2\core.hpp> #include <opencv2\highgui.hpp> #include <opencv2\imgproc. ...
- Python中的sin和cos函数
1 第一次使用math.sin()和math.cos(),可是发现结果不对,比如Math.sin(90)=0.893996663600,奇怪? 2 3 一查,原来sin(x) \n\n Ret ...
- GM MDI Tech 3 VS GM tech 2
Many customers ask for this question: what is the difference between GM tech 2 and GM MDI Tech 3 sca ...
- Win2003 控制面板 打不开
windown 2003 控制面板打不开 在运行框中输入regedit,进入注册表中的 HKEY_LOCAL_MACHINE/SYSTEM\Current ControlSet\Control\NLS ...
- python_文件操作代码实例
"""提示:代码中的内容均被注释,请参考,切勿照搬""" 1 #文件的打开和关闭 ''' 文件对象 = open('文件名','使用方式') ...
- Turing Year 2012
Turing LectureFrom cryptanalysis to cognitive neuroscience - a hidden legacy of Alan Turinghttp://co ...
- CI控制器调用内部方法并载入相应模板的做法
当我打开链接:http://localhost/3g/index/open/a/b?from=timeline后,判断链接中的from是否等于timeline,如果等于timeline,那么就调用控制 ...
- HDU 5441——Travel——————【并查集+二分查界限】
Travel Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Su ...
- 让你分分钟学会 javascript 闭包
闭包,是 javascript 中重要的一个概念,对于初学者来讲,闭包是一个特别抽象的概念,特别是ECMA规范给的定义,如果没有实战经验,你很难从定义去理解它.因此,本文不会对闭包的概念进行大篇幅描述 ...
- 简单的JS统计静态网站访问次数
最简单的静态页面刷新 <html> <head> <meta http-equiv="Content-Type" content="text ...