简体   繁体   English

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

[英]How to center a fixed sized main layout in android?

I want my layout to be fixed sized say: 320dp by 480dp as a default for ldpi screen resolution. 我希望我的布局固定大小说:320dp×480dp作为ldpi屏幕分辨率的默认值。

So when the user has a 720x1280 device the layout would still be 320x480 but would be centered. 因此,当用户使用720x1280设备时,布局仍然是320x480,但会居中。

Any suggestions? 有什么建议么? I want to avoid creating multiple layouts for each resolution. 我想避免为每个分辨率创建多个布局。

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

Hope this is what you want. 希望这是你想要的。

For your parent view container, you should just set the layout_width and layout_height to the desired size and then make its layout_gravity equal to center. 对于父视图容器,您应该将layout_width和layout_height设置为所需的大小,然后使其layout_gravity等于center。 So, in your case you would use the following 因此,在您的情况下,您将使用以下内容

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

That should center the view and keep it the desired size. 这应该使视图居中并保持所需的大小。 It should be noted that you can use any layout, not just LinearLayout. 应该注意的是,您可以使用任何布局,而不仅仅是LinearLayout。

Hope this helps! 希望这可以帮助! Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM