繁体   English   中英

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

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

我希望我的布局固定大小说:320dp×480dp作为ldpi屏幕分辨率的默认值。

因此,当用户使用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>

希望这是你想要的。

对于父视图容器,您应该将layout_width和layout_height设置为所需的大小,然后使其layout_gravity等于center。 因此,在您的情况下,您将使用以下内容

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

希望这可以帮助! 祝好运。

暂无
暂无

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

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