2013-10-16 42 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> 

希望这是你想要的。