【整理】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 Server 2012 群集共享卷
群集共享卷,简单理解就是一个针对Hyper-V优化的一个分布式访问文件系统,群集中的节点可以同时写到一个磁盘,并且是受控访问,以避免写数据的冲突.此功能和Vmware 的VMFS是类似的.此功能从W ...
- ARM汇编指令集3
常用ARM指令1:数据处理指令 •数据传输指令 mov mvn mov r1, r0 @两个寄存器之间数据传递 mov r1, #0xff ...
- python 魔法方法补充(__setattr__,__getattr__,__getattribute__)
python 魔法方法补充 1 getattribute (print(ob.name) -- obj.func())当访问对象的属性或者是方法的时候触发 class F(object): def _ ...
- include和application
include指令 语法:<%@ include file=”路径+文件名” %> 把指定的文件包含到当前jsp中. application(应用的全局变量) 实现用户之间的数据共享 常 ...
- 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 ...
- 动态加载css、js引用
在js代码中动态的加载js.css文件的引用 function addJsCssByLink(type,url) { var doc=document; if(type="js") ...
- 21天学通C++_Day4
0.迭代器 昨天晚上3G移动通信实验的时候,需要写一些简单的C程序,用到for循环的时候,发现在不同的for循环中,若定义标识符相同的变量名时,会有报错,环境是VC6: 可是一想到在for语句声明的迭 ...
- 【WCF安全】SOAP消息实现用户名验证:通过OperationContext直接添加/访问MessageHeader信息
服务代码 1.契约 using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Se ...
- WPF 自定义绕圈进度条(转)
在设计界面时,有时会遇到进度条,本次讲解如何设计自定义的绕圈进度条,直接上代码: 1.控件界面 <UserControl x:Class="ProgressBarControl&quo ...