Android的布局文件中,如果想讓1個組件(布局或View)居中顯示在另外一個布局(組件)中,可以由這么幾種做法:
android:layout_gravity,用來指定當前組件(布局或View)在父組件(布局)中的位置,父布局應當是LinearLayout或它的后裔。
layout_gravity取值多是:
與居中相干的已粗體標注出來。各種取值的具體含義,參看:https://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html。
android:gravity是View的屬性,用來指定View的子組件在View中的位置。適用于所有的View和布局。它的取值有很多,具體參見:https://developer.android.com/reference/android/view/Gravity.html。
與居中相干的取值:
當你設(shè)定1個布局android:gravity="center"
時,它的子組件就會居中。當你設(shè)定1個Viewandroid:gravity="center"
時,它的內(nèi)容會居中,以TextView為例,文字會居中。
android:layout_centerInParent是RelativeLayout的布局屬性,如果1個組件(布局或View)的父布局是RelativeLayout,就能夠使用這個屬性來居中。其取值為 true
或 false
。
與其類似的還有:
具體含義和用法參看:https://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html。