Can you center a Button in RelativeLayout?


Question

I'm trying to center a button in relative layout, is this possible? I've tried the Gravity and Orientation functions but they don't do anything.

1
161
9/20/2010 2:20:49 AM

Try

android:layout_centerHorizontal="true"

Exactly like this, it works for me:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="#ff0000">

    <Button
        android:id="@+id/btn_mybutton"
        android:layout_height="wrap_content"
        android:layout_width="124dip"
        android:layout_marginTop="5dip"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>
347
10/10/2014 5:08:03 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon