如何在android中居中放置固定大小的主布局?

如何在android中居中放置固定大小的主布局?,android,android-layout,Android,Android Layout,我希望我的布局是固定大小的,比如说:320dp乘以480dp作为ldpi屏幕分辨率的默认值 <?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" &g

我希望我的布局是固定大小的,比如说:320dp乘以480dp作为ldpi屏幕分辨率的默认值

<?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>
因此,当用户拥有720x1280设备时,布局仍将为320x480,但将居中

有什么建议吗?我希望避免为每个分辨率创建多个布局。


<?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>

希望这是您想要的。

对于父视图容器,您应该将布局宽度和布局高度设置为所需的大小,然后使其布局重心等于中心。因此,在您的情况下,您将使用以下

<?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>

这将使视图居中并保持所需的大小。需要注意的是,您可以使用任何布局,而不仅仅是线性布局


希望这有帮助!祝你好运。

如果我想在大屏幕上显示视图时提供背景,那么我必须嵌套布局?这是一种方法。另一个(可能更好的)解决方案是研究风格和主题。请特别注意android:windowBackground