2013-10-16 46 views
0

我希望我的佈局是固定大小,例如:320dp,480dp作爲ldpi屏幕分辨率的默認值。如何在android中居中固定大小的主佈局?

所以當用戶有一個720x1280設備時,佈局仍然是320x480,但會居中。

有什麼建議嗎?我想避免爲每個分辨率創建多個佈局。

回答

1

對於您的父視圖容器,您應該將layout_width和layout_height設置爲所需的大小,然後使其layout_gravity等於中心。所以,你的情況,你可以使用以下

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="320dp" 
    android:layout_height="480dp" 
    android:layout_gravity="center" > 

    <!-- The rest of your layout --> 

</LinearLayout> 

這應該市中心的景色,並保持它所需的大小。應該指出,你可以使用任何佈局,而不僅僅是LinearLayout。

希望這會有所幫助!祝你好運。

+0

所以我將不得不嵌套佈局,如果我想提供一個背景時,視圖是在一個更大的屏幕? – Arcadian

+0

這將是一種方法。另一個(也可能更好的)解決方案是查看樣式和主題。 [這是一個鏈接](http://developer.android.com/guide/topics/ui/themes.html#ApplyATheme)。請特別注意android:windowBackground –

1
<?xml version="1.0" encoding="UTF-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <RelativeLayout android:layout_width="320px" 
     android:layout_height="480px" 
     android:layout_centerInParent="true" 
     android:background="@color/white"> 

    </RelativeLayout> 

</RelativeLayout> 

希望這是你想要的。