Search code examples
androidlayoutcenterandroid-relativelayoutcentering

another relative layout centering issue


I have a problem centering a OK button at the end of a Relative layout, i've checked a lot of similar questions, but couldn't find my mistake. whatever i try, the OK button is still offset from the center.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frag_choix_surface"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:orientation="vertical" >

<TextView
    android:id="@+id/textViewNbPieces"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:text=""
    android:textStyle="bold" />

<LinearLayout
    android:id="@+id/pieces"
    android:layout_marginTop="20dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textViewSurface"
    android:gravity="center"
     >

    <TextView
        android:id="@+id/textViewChmin"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="10dp"
        android:gravity="center"
        android:text="min :" />

    <kankan.wheel.widget.WheelView android:id="@+id/nb_piece_min"
        android:layout_height="wrap_content"
        android:layout_width="50dp"/>

    <TextView
        android:id="@+id/textViewChmax"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:gravity="center"
        android:text="max :" >

    </TextView>

    <kankan.wheel.widget.WheelView android:id="@+id/nb_piece_max"
        android:layout_height="wrap_content"
        android:layout_width="50dp"
        android:layout_marginRight="40dp"/>

</LinearLayout>

<Button
    android:id="@+id/buttonOkNbPieces"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/pieces"
    android:layout_marginTop="20dp"
    android:text="OK" />

</RelativeLayout>

Sorry i couldn't include a screenshot, i'm fresh new to the site, and don't have any rep! Any ideas?


Solution

  • In RelativeLayout you have to use android:layout_centerHorizontal, android:layout_centerVertical or android:layout_centerInParent attributes.

    Change the button part of the xml to:

    <Button
        android:id="@+id/buttonOkNbPieces"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/pieces"
        android:layout_marginTop="20dp"
        android:layout_centerHorizontal="true"
        android:text="OK" />
    

    And remove attributes android:gravity, android:layout_marginLeft and android:layout_marginRight from the parent RelativeLayout, these attributes affects the center position of the inner items. So the RelativeLayout should be defined like this:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/frag_choix_surface"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp" >