Ring
Ring

Reputation: 2309

Center This Android Layout?

I can't for the life of me figure out how to get my box centered on BOTH the horizontal and vertical.

Download Google Chrome to see this image.

Heres my xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/dragon"
    android:orientation="vertical" >
        
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/playerinfosquare"
        android:orientation="vertical">

        <TextView
            android:id="@+id/spellinfotitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_margin="10px"
            android:text="Spellventory"
            android:textAppearance="?android:attr/textAppearanceLarge" />
        
        <TextView
            android:id="@+id/spell1info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_margin="10px"
            android:text="No Spell Equipped"
            android:textAppearance="?android:attr/textAppearanceMedium" />
        
        <TextView
            android:id="@+id/spell2info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_margin="10px"
            android:text="No Spell Equipped"
            android:textAppearance="?android:attr/textAppearanceMedium" />
        
        <TextView
            android:id="@+id/spell3info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_margin="10px"
            android:text="No Spell Equipped"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    
    </LinearLayout>

</LinearLayout>

Help?

Are you kidding me? Stack overflow thinks it knows better than me and says that I don't have enough content to explain my code sections. Well, here you go! This is what you get for having arbitrary rules that don't always make sense.

Upvotes: 1

Views: 192

Answers (2)

twaddington
twaddington

Reputation: 11645

You need to set android:gravity="center" on your first LinearLayout.

Upvotes: 2

Shellum
Shellum

Reputation: 3179

Put your second LinearLayout into a RelativeLayout. You can then center it much easier by setting

android:layout_centerInParent="true"

on the second LinearLayout

Upvotes: 4

Related Questions