Arcadian
Arcadian

Reputation: 4350

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.

So when the user has a 720x1280 device the layout would still be 320x480 but would be centered.

Any suggestions? I want to avoid creating multiple layouts for each resolution.

Upvotes: 0

Views: 66

Answers (2)

Andrew Schuster
Andrew Schuster

Reputation: 3229

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

Hope this helps! Good luck.

Upvotes: 1

Triode
Triode

Reputation: 11359

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

Upvotes: 1

Related Questions