1. 程式人生 > >LinearLayout 中設定button水平垂直居中

LinearLayout 中設定button水平垂直居中

一共巢狀兩層linearlayout。最外層linearlayout設定內部垂直排列,之後設定第二層linearlayout設定水平居中,內部水平排列。設定第二層linearlayout寬度為wrap_content,高度為match_parent,以為寬度為所設定button的寬度,高度應為父佈局的高度,否則不能設定出水平居中的效果。

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_below="@+id/title_bar"
android:orientation="vertical" android:background="#aaaaaa">
<LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal" android:layout_gravity="center_horizontal"
>
<Button android:id="@+id/person_photo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="頭像"/> </LinearLayout>
</LinearLayout>