先上图和代码
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="#ffff00" 6 android:gravity="bottom"> 7 8 <Button 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:gravity="center" 12 android:text="点击" 13 android:textSize="40sp" /> 14 </LinearLayout>
android:gravity这个是针对控件里的元素来说的,用来控制元素在该控件里的显示位置。
上面有两个android:gravity,分别在第6行和第11行,以下分别说明。
对于LinearLayout这个控件来说,Button是它的元素,所以当第6行的android:gravity的值为bottom的时候,Button的位置是屏幕的底部。
对于Button这个控件来说,它的内容(text)是他的元素,所以当第11行的android:gravity的值为center的时候,文字是居中显示的。