pierrotlefou
pierrotlefou

Reputation: 40761

android:layout_gravity will put child view in center vertically but not horizontally

The layout XML is :

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/translucenter_dark" >
    <LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="200dip"
    >
    <ProgressBar android:id="@+id/progressBar1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center"

    ></ProgressBar>
    </LinearLayout>

</LinearLayout>

And the final layout is :

enter image description here

Why the progressBar was not put in center both vertically and horizontally. Is it a feature or a bug :)

Upvotes: 1

Views: 4382

Answers (1)

blessanm86
blessanm86

Reputation: 31789

You could give the linear layout enclosing the progress bar android:gravity="center". This will make the progress bar centered.

It seems progress bar utilizes the ViewGroup.LayoutParams. This class does not support layout_gravity such as LinearLayout.LayoutParams. So you will require to use the gravity property of the parent linear layout. This is a pretty good explanation of gravity in android.

Upvotes: 6

Related Questions