关于android gravity属性,weight属性,layout_gravity属性的使用

android gravity属性  weight属性 layout_gravity属性

来看这个布局文件 

复制代码
 1 <?xml version="1.0" encoding="utf-8"?>  
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
 3     android:orientation="vertical"  
 4     android:layout_width="fill_parent"  
 5     android:layout_height="fill_parent"  
 6     >  
 7 <EditText  
 8     android:layout_width="fill_parent"  
 9     android:layout_height="wrap_content"  
10     android:text="one"/>  
11 <EditText  
12     android:layout_width="fill_parent"  
13     android:layout_height="wrap_content"  
14     android:text="two"/>  
15     <EditText  
16     android:layout_width="fill_parent"  
17     android:layout_height="wrap_content"  
18     android:text="three"/>  
19 </LinearLayout>  
复制代码

这是一个很正常的布局文件了,效果如下。 

 

当我们给这三个EditText设置上gravity属性之后效果如下,先看 xml文件。 

复制代码
 1 <?xml version="1.0" encoding="utf-8"?>  
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
 3     android:orientation="vertical"  
 4     android:layout_width="fill_parent"  
 5     android:layout_height="fill_parent"  
 6     >  
 7 <EditText  
 8     android:layout_width="fill_parent"  
 9     android:layout_height="wrap_content"  
10     android:gravity="left"  
11     android:text="one"/>  
12 <EditText  
13     android:layout_width="fill_parent"  
14     android:layout_height="wrap_content"  
15     android:gravity="center"  
16     android:text="two"/>  
17     <EditText  
18     android:layout_width="fill_parent"  
19     android:layout_height="wrap_content"  
20     android:gravity="right"  
21     android:text="three"/>  
22 </LinearLayout>  
复制代码

 

由此可以看出gravity属性是用来控制 EditText里边文本的位置。 

   我们现在对xml文件做如下修改 

复制代码
 1 <?xml version="1.0" encoding="utf-8"?>  
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
 3     android:orientation="vertical"  
 4     android:layout_width="fill_parent"  
 5     android:layout_height="fill_parent"  
 6     >  
 7 <EditText  
 8     android:layout_width="fill_parent"  
 9     android:layout_height="wrap_content"  
10     android:gravity="left"  
11     android:text="one"/>  
12 <EditText  
13     android:layout_width="fill_parent"  
14     android:layout_height="wrap_content"  
15     android:gravity="center"  
16     android:layout_weight="1.0"  
17     android:text="two"/>  
18     <EditText  
19     android:layout_width="fill_parent"  
20     android:layout_height="wrap_content"  
21     android:gravity="right"  
22     android:text="three"/>  
23 </LinearLayout> 
复制代码

  运行效果如下 

 

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

关于 android:layout_gravity和android:gravity属性的区别


一、介绍:

   gravity的中文意思就是”重心“,就是表示view横向和纵向的停靠位置

  (1).android:gravity:是对view控件本身来说的,是用来设置view本身的内容应该显示在view的什么位置,默认值是左侧。也可以用来设置布局中的控件位置

  (2).android:layout_gravity:是相对于包含改元素的父元素来说的,设置该元素在父元素的什么位置;

  比如TextView: android:layout_gravity表示TextView在界面上的位置,android:gravity表示TextView文本在TextView的什么位置,默认值是左侧.

二、使用过程中失效

  • android:gravity : 表示当前View,即控件,内部的东西的,对齐方式
    • 常见的是:
      • TableRow中的Button
      • EditText(内部)的文字
      • Button(内部)的文字
  • android:layout_gravity: 表示当前View,即控件本身在父一级内的(即父一级控件所给当前子控件所分配的显示范围内)的对齐方式
    • 常见的是:
      • 当前EditText(在父一级LineLayout所分配给其的显示范围内)的对齐方式
      • 当前的Button(在父一级TableRow所分配给其的显示范围内)的对齐方式 ->此处需要注意的是,很多时候,改变Button内的layout_gravity,常看不到改动的效果,是因为其显示范围和位置,已经由父一级的TableRow的gravity决定了。
      • 下面的代码是设置button的显示效果
复制代码
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6 
 7     <Button
 8         android:layout_width="250dp"
 9         android:layout_height="wrap_content"
10         android:layout_gravity="right"
11         android:gravity="right"
12         android:text="文字的gravity属性" />
13 
14 </LinearLayout>
复制代码

结果如下:

可以看得出来button的位置由于layout_gravity的right属性位于view的右侧,而该button的内容也由于gravity的设置位于了右侧。

但是当修改了外层的linearlayout的orientation属性时:就会发生变化

复制代码
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="horizontal" >
 6 
 7     <Button
 8         android:layout_width="250dp"
 9         android:layout_height="wrap_content"
10         android:layout_gravity="right"
11         android:gravity="right"
12         android:text="文字的gravity属性" />
13 
14 </LinearLayout>
复制代码

可以看得出来此处android:layout_gravity="right"属性设置无效。

通过验证得出来:

        (1) 当我们的LinearLayout的orientation的属性值是horizontal也就是水平方向时:--->我们可以设置控件的layout_gravity属性值为:竖直方向的变化比如bottom,top,center_vertical。

        (2) 当我们的LinearLayout的orientation的属性值是vertical也就是竖直方向是:--->我们可以设置控件的layout_gravity属性值为:水平方向的变化比如right,left,center_horizontal。

        (3)这个结论表明,layout_gravity属性时子控件相对于父布局的相对位置,一旦父布局写死了方向水平或者垂直,在该方向上就只能是默认变化,子控件无法干预,但是在与之相对的方向上子控件可以自由变幻,想想也是可以理解的,老子的方向订好了,你只能是往前一点或者往后一点,想直接达到终点这不行,但是允许你出去找小朋友玩耍~~~

        (4)有一个比较特殊的是center,不管是横向还是纵向的时候,它总有一个方向起作用

 

  • android:gravity : 表示当前View,即控件,内部的东西的,对齐方式
    • 常见的是:
      • TableRow中的Button
      • EditText(内部)的文字
      • Button(内部)的文字
  • android:layout_gravity: 表示当前View,即控件本身在父一级内的(即父一级控件所给当前子控件所分配的显示范围内)的对齐方式
    • 常见的是:
      • 当前EditText(在父一级LineLayout所分配给其的显示范围内)的对齐方式
      • 当前的Button(在父一级TableRow所分配给其的显示范围内)的对齐方式 ->此处需要注意的是,很多时候,改变Button内的layout_gravity,常看不到改动的效果,是因为其显示范围和位置,已经由父一级的TableRow的gravity决定了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值