从名字上可以看到,android:gravity是对元素本身说的,元素本身的文本显示在什么地方靠着换个属性设置,不过不设置默认是在左侧的。android:layout_gravity是相对与它的父元素说的,说明元素显示在父元素的什么位置。

我们举一个例子大家看一下:

<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="gravity水平居中"
android:textSize="20dip"
android:gravity="center_horizontal"
android:background="#666" />
<ImageButton
android:layout_marginTop="10dip"
android:layout_marginBottom="10dip"
android:id="@+id/ImageButton01"
android:src="@drawable/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
</ImageButton>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="layout_gravity局右"
android:textSize="20dip"
android:background="#666" />
</LinearLayout>

显示效果如下,可以体会一下之间的区别:

本讲就到这里,欢迎大家和我交流。

转载:http://android.yaohuiji.com/archives/293

更多相关文章

  1. Android(安卓)UI学习1:控件和基本事件的响应
  2. Android中_TextView属性的XML详解 包括单行显示等等。
  3. Android(安卓)Window类
  4. 初次体验Android,过程很艰辛!
  5. 最新!!Android(安卓)状态栏详细开发,5分钟精通状态栏
  6. android style(样式)和theme(主题)设置
  7. Android(安卓)activity intent 入门
  8. .Android的Window类默认分类
  9. android 线性布局几个属性

随机推荐

  1. android 读取一个文件下所有的文件
  2. android animation中的参数interpolator
  3. android ndk不支持宽字符转换
  4. 使用valgrind检测Android(安卓)native程
  5. android 5.1 屏蔽MENU键 返回键唤醒系统
  6. 【总结备用】Android监听网络状态实现(Br
  7. Android使用SQLite数据库
  8. Android(安卓)属性动画Property Animatio
  9. 重点核心☆☆☆☆☆按钮点击事件的四种类
  10. 基于Android(安卓)studio3.6的JNI教程之n