New UI-gravity和layout_gravity属性解析

New UI-gravity和layout_gravity属性解析

 ——转载请注明出处:coder-pig,欢迎转载,请勿用于商业用途!


小猪Android开发交流群已建立,欢迎大家加入,无论是新手,菜鸟,大神都可以,小猪一个人的

力量毕竟是有限的,写出来的东西肯定会有很多纰漏不足,欢迎大家指出,集思广益,让小猪的博文

更加的详尽,帮到更多的人,O(∩_∩)O谢谢!

小猪Android开发交流群:小猪Android开发交流群群号:421858269

新Android UI实例大全目录:http://blog.youkuaiyun.com/coder_pig/article/details/42145907




1)两者用法演示:

相信大家对于这两个属性都不陌生吧,也是这两个其实平时用的很多的:

android:gravity:设置view中内容的对齐方式!

android:layout_gravity:设置view在布局容器中的对齐方式,要在LinearLayout里面才可以用哦!


多说无益,通过一个简单的例子就可以知道了

两个简单的TextView,设置不同的gravity和layout_gravity属性


布局代码如下:

[html]   view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context="com.jay.example.getscreendemo.MainActivity"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <TextView  
  9.         android:layout_gravity="right"  
  10.         android:background="#FF7878"  
  11.         android:gravity="center"  
  12.         android:layout_width="300dp"  
  13.         android:layout_height="200dp"  
  14.         android:text="O(∩_∩)O哈哈~"   
  15.         android:textSize="18sp"/>  
  16.       
  17.     <TextView  
  18.         android:background="#FF7428"  
  19.         android:gravity="left|bottom"  
  20.         android:layout_width="300dp"  
  21.         android:layout_height="200dp"  
  22.         android:text="(*^__^*) 嘻嘻……"  
  23.         android:textSize="18sp" />  
  24.   
  25. </LinearLayout><strong>  
  26. </strong>  

用法都是很简单的,可选的值都是一样的,当然可以同时设置多个属性,只需用|间隔即可,比如左上left|top




2)使用Layout_gravity的一个很重要的问题!!!

这个问题是一个读者偶尔一次发现反馈给我的,万分感谢,同时希望大家在看小猪博客的时候可以提出

一些实际开发中遇到的问题,以及解决方式,好给后来者经验!毕竟小猪不是神,不是什么方方面面都能

考虑到的,谢谢!


问题内容:

在一个LinearLayout的水平方向中布置两个TextView,想让一个左,一个右,怎么搞?

或许你会脱口而出:"gravity设置一个left,一个right就可以啦!"

真的这么简单?你试过吗?写个简单的Layout你就会发现,事与愿违了:

[html]   view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal"  
  6.     tools:context="com.jay.example.getscreendemo.MainActivity" >  
  7.   
  8.     <TextView  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="200dp"  
  11.         android:layout_gravity="left"  
  12.         android:background="#FF7878"  
  13.         android:gravity="center"  
  14.         android:text="O(∩_∩)O哈哈~" />  
  15.   
  16.     <TextView  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="200dp"  
  19.         android:layout_gravity="right"  
  20.         android:background="#FF7428"  
  21.         android:gravity="center"  
  22.         android:text="(*^__^*) 嘻嘻……" />  
  23.   
  24. </LinearLayout>  
运行结果图:


看到这里你会说:哎呀,真的不行耶,要不在外层LinearLayout加个gravity=left的属性,然后设置第二个

TextView的layout_gravity为right,恩,好我们试一下:

[html]   view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal"  
  6.     android:gravity="left"  
  7.     tools:context="com.jay.example.getscreendemo.MainActivity" >  
  8.   
  9.     <TextView  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="200dp"  
  12.         android:background="#FF7878"  
  13.         android:gravity="center"  
  14.         android:text="O(∩_∩)O哈哈~" />  
  15.   
  16.     <TextView  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="200dp"  
  19.         android:layout_gravity="right"  
  20.         android:background="#FF7428"  
  21.         android:gravity="center"  
  22.         android:text="(*^__^*) 嘻嘻……" />  
  23.   
  24. </LinearLayout>  
结果还是一样:



好吧,没辙了,怎么办好?

当 android:orientation="vertical" 时, 只有水平方向的设置才起作用,垂直方向的设置不起作用。

即:left,right,center_horizontal 是生效的。

当 android:orientation="horizontal" 时, 只有垂直方向的设置才起作用,水平方向的设置不起作用。

即:top,bottom,center_vertical 是生效的


不过貌似这个解决方法有点坑爹,比如如果只能竖直方向设置左右对齐的话,就会出现下面的效果:


显然不是我们要的结果把!

综上,要么按照上述给出的规则来布局,不过对于这种情况还是使用相对布局RelativeLayout把!

网上没给出具体的原因,都是说这样改有人说这个和orientation的优先级有关

,暂且先mark下来吧,后续如果知道原因的话再解释!前面屏幕适配也说过了,布局还是建议使用

RelativeLayout!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值