簡體   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