【整理】Android中的gravity和layout_gravity区别
【背景】
在Android中,想要设置个按钮的水平对齐,都累死了:
【已解决】ADT中已设置TableLayout布局的情况下如何设置按钮居中对齐 所以现在有必要搞清楚,到底gravity和layout_gravity到底有啥区别。
1.参考:
Android – gravity and layout_gravity
Android中gravity与layout_gravity的区别
中的解释,可以总结为:
- android:gravity : 表示当前View,即控件,内部的东西的,对齐方式
- TableRow内部的Button
- 右对齐:
- 代码:
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"> <Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:layout_gravity="center"
android:onClick="preformDownload"
android:text="@string/btn_download" />
</TableRow>
- 效果:

- 居中对齐:
- 代码:
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"> <Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:layout_gravity="center"
android:onClick="preformDownload"
android:text="@string/btn_download" />
</TableRow>
- 效果:

- Button内部的文字(Text):
- 垂直方向:顶端Top
- 代码:
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom"
android:gravity="top"
android:onClick="preformDownload"
android:text="@string/btn_download" />
- 效果:

- 垂直方向:居中对齐:
- 代码:
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom"
android:gravity="center_vertical"
android:onClick="preformDownload"
android:text="@string/btn_download" />
- 效果:

- android:layout_gravity: 表示,当前View,即控件,本身,(在父一级的控件所分配的显示范围内)的对齐方式
- EditText(在父一级的LinearLayout内)
- EditText居中对齐:
- 代码:
<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="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="one" /> </LinearLayout>
- 效果:

- EditText右对齐:
- 代码:
<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="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="center"
android:text="one" /> </LinearLayout>
- 效果:

【总结】
- android:gravity : 表示当前View,即控件,内部的东西的,对齐方式
- 常见的是:
- TableRow中的Button
- EditText(内部)的文字
- Button(内部)的文字
- android:layout_gravity: 表示当前View,即控件本身,在父一级内的(即父一级控件所给当前子控件所分配的显示范围内)的对齐方式
- 常见的是:
- 当前EditText(在父一级LineLayout所分配给其的显示范围内)的对齐方式
- 当前的Button(在父一级TableRow所分配给其的显示范围内)的对齐方式 ->此处需要注意的是,很多时候,改变Button内的layout_gravity,常看不到改动的效果,是因为其显示范围和位置,已经由父一级的TableRow的gravity决定了。
总之,拿着代码,多试试,就容易理解了。
【整理】Android中的gravity和layout_gravity区别的更多相关文章
- Android中style和theme的区别
在学习Xamarin android的过程中,最先开始学习的还是熟练掌握android的六大布局-LinearLayout .RelativeLayout.TableLayout.FrameLayou ...
- Android中Popupwindow和Dialog的区别
Android中的对话框有两种:PopupWindow和AlertDialog.它们都可以实现弹窗功能,但是他们之间有一些差别,下面总结了一点. (1)Popupwindow在显示之前一定要设置宽高, ...
- Android中px和dip的区别
在Android手机的诞生之初,由于Android系统是开源的,一开始便有众多的OEM厂商对Android手机进行深度定制,于是乎Android手机的皮肤和屏幕大小都变得百花齐放,这可苦逼了我们这群开 ...
- 浅谈Android中Serializable和Parcelable使用区别
版权声明:本文出自汪磊的博客,转载请务必注明出处. 一.概述 Android开发的时候,我们时长遇到传递对象的需求,但是我们无法将对象的引用传给Activity或者Fragment,我们需要将这些对象 ...
- 【Android】android中Invalidate和postInvalidate的区别
Android中实现view的更新有两组方法,一组是invalidate,另一组是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用. Android提供了Inva ...
- android中Invalidate和postInvalidate的区别
Android中实现view的更新有两组方法,一组是invalidate,另一组是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用. Android提供了Inva ...
- [转]Android 中fill_parent与wrap_content的区别
在Android中,对于组件的属性“layout_width”和“layout_height”, 其值总是设置为“wrap_content”或“fill_parent”. 那么,这两个值有什么 ...
- Android中Bundle和Intent的区别
Bundle的作用,以及和Intent的区别: 一.Bundle: A mapping from String values to various Parcelable types 键值对的集合 类继 ...
- android中接口和抽象类的区别
最近发现很多基础有点生疏了,特地写一点博客来巩固一下.今天主要来谈谈接口和抽象类的区别,我们在项目的很多地方都会用到接口或者抽象类,但是它们之间的一些区别和相同点不知道大家有没有注意到,还有就是,什么 ...
随机推荐
- 注册表操作的几个windows api
(转自:http://blog.sina.com.cn/s/blog_4e66c6cd01000bcu.html) 键管理类: RegCloseKey():关闭注册表键释放句柄. RegC ...
- LeetCode OJ :Remove Linked List Elements (移除链表元素)
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- Java中会有内存泄露吗?
一.Java内存回收机制 不论哪种语言的内存分配方式,都需要返回所分配内存的真实地址,也就是返回一个指针到内存块的首地址.Java中对象是采用new或者反射的方法创建的, 这些对象的创建都是在堆(He ...
- 【python】使用asyncore进行异步通信
参考博文:http://blog.csdn.net/livefun/article/details/8721772 参考博文:https://www.cnblogs.com/tomato0906/ar ...
- 关于bind named.conf.options
/etc/named/named.conf.options中的Options参数 listen-on port 53 { any; }; 监听在这部主机系统上面的哪个网路介面.预设是监听在loca ...
- Zookeeper集群是如何升级到新版本的
方案1:复用老数据方案 这是经过实践的升级方案,该方案是复用旧版本的数据,zk集群拓扑,配置文件都不变,只是启动的程序为最新的版本. 参考文章: Zookeeper集群是如何升级到新版本的 方案2:重 ...
- matlab批量转化img到gray
path = 'D:\宝贝儿数据集\COIL-3D\coil-100\'; save_path = 'D:\宝贝儿数据集\COIL-3D\coil-100-gray\'; file=dir([path ...
- Leetcode 999. Available Captures for Rook
class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: rook = [0, 0] ans = 0 f ...
- ranch实现游戏服务器
在 erlang游戏开发tcp 我们建立起了自己的socket tcp 服务器的基本骨架.当时面对并发情况下,多人同一时刻连接服务器的时候,我们的基本骨架 还是难以应付处理.这就使我不得不想对这样的情 ...
- 【转】C# Socket编程(1)基本的术语和概念
[转自:http://www.cnblogs.com/IPrograming/archive/2012/10/10/2718371.html#Protocol] 计算机程序能够相互联网,相互通讯,这使 ...