2010-10-03 133 views
0

爲什麼下面的代碼不與中心對齊? 我需要做些什麼來修復它?在Android中使用分層佈局與中心對齊

<AbsoluteLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_gravity="center" 
    android:id="@+id/parent" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:background="@color/blue"> 

<RelativeLayout 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_width="wrap_content"> 


    <Button 
     android:layout_gravity="center" 
     android:id="@+id/Question01" 
     android:text="12 + 23" 
     android:gravity="center_vertical|center_horizontal" 
     android:layout_height="70px" 
     android:lines="1" 
     android:textSize="40px" 
     android:layout_alignWithParentIfMissing="true" android:background="@drawable/orange_button" android:layout_margin="5px" android:layout_width="230px" android:textColor="@color/blue"/> 


</RelativeLayout> 
</AbsoluteLayout> 

回答

3

嘛,android:layout_gravity是不是子視圖的定位,它是關於其母公司中當前視圖的定位。嘗試使用android:gravity作爲AbsoluteLayout(也可能要切換到FrameLayout而不是Abdolute)。對於RelativeLayout,請在按鈕上使用android:layout_centerInParent="true"

<AbsoluteLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:gravity="center" 
android:id="@+id/parent" 
android:layout_height="match_parent" 
android:layout_width="match_parent" 
android:background="@color/blue"> 
<RelativeLayout 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"> 
<Button 
    android:layout_gravity="center" 
    android:id="@+id/Question01" 
    android:text="12 + 23" 
    android:gravity="center_vertical|center_horizontal" 
    android:layout_height="70px" 
    android:lines="1" 
    android:textSize="40px" 
    android:layout_alignWithParentIfMissing="true" 
    android:background="@drawable/orange_button" 
    android:layout_margin="5px" android:layout_width="230px" 
    android:textColor="@color/blue" 
    android:layout_centerInParent="true"/> 
</RelativeLayout> 
</AbsoluteLayout> 
+2

+1不使用'AbsoluteLayout' – 2010-10-03 15:06:32