先上图和代码
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent"> 4 5 <TextView 6 android:layout_width="fill_parent" 7 android:layout_height="wrap_content" 8 android:layout_gravity="center" 9 android:background="#ff00ff" 10 android:gravity="end" 11 android:text="layout_gravity" 12 android:textSize="40sp" /> 13 </LinearLayout>
个人理解,android:layout_gravity表示控件相对于父控件(容器)的位置。
上图中,第8行android:layout_gravity="center",所以,Textview在其父控件LinearLayout中是居中的。
另外,复习一下android:gravity,它表示控件的元素在控件中的位置,所以Textview中的字符串是靠右显示的。
原文:http://www.cnblogs.com/jimichen/p/4944805.html