Android的布局文件中,如果想讓一個組件(布局或View)居中顯示在另一個布局(組件)中,可以由這么幾種做法:
layout_gravity
android:layout_gravity ,用來指定當前組件(布局或View)在父組件(布局)中的位置,父布局應該是LinearLayout或者它的后裔。
layout_gravity取值可能是:
與居中相關的已經粗體標注出來。各種取值的具體含義,參看:
https://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html 。
gravity
android:gravity 是View的屬性,用來指定View的子組件在View中的位置。適用于所有的View和布局。它的取值有很多,具體參見: https://developer.android.com/reference/android/view/Gravity.html 。
與居中相關的取值:
當你設定一個布局 android:gravity="center" 時,它的子組件就會居中。當你設定一個View android:gravity="center" 時,它的內容會居中,以TextView為例,文字會居中。
layout_centerInParent
android:layout_centerInParent 是RelativeLayout的布局屬性,如果一個組件(布局或View)的父布局是RelativeLayout,就可以使用這個屬性來居中。其取值為 true 或 false 。
與其類似的還有:
具體含義和用法參看: https://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html 。
以上就是對Android 布局居中的幾種方法整理,后續(xù)繼續(xù)補充相關資料,謝謝大家對本站的支持!
新聞熱點
疑難解答