Can you center a Button in RelativeLayout?

前端 未结 11 1260
粉色の甜心
粉色の甜心 2020-12-12 13:35

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.

相关标签:
11条回答
  • 2020-12-12 13:53

    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>
    
    0 讨论(0)
  • 2020-12-12 13:55

    Removing any alignment like android:layout_alignParentStart="true" and adding centerInParent worked for me. If the "align" stays in, the centerInParent doesn't work

    0 讨论(0)
  • 2020-12-12 14:01

    It's really easy. Try the code below,

    <RelativeLayout
        android:id="@+id/second_RL"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/first_RL"
        android:background="@android:color/holo_blue_bright"
        android:gravity="center">
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-12-12 14:03

    Its easy, dont Align it to anything

    <Button
            android:id="@+id/the_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_centerInParent="true"
            android:text="Centered Button"/>

    0 讨论(0)
  • 2020-12-12 14:04

    You can use CENTER_IN_PARENT for relative layout.

    Add android:layout_centerInParent="true" to element which you want to center in the RelativeLayout

    0 讨论(0)
  • 2020-12-12 14:05

    To center align in one of the direction use android:layout_centerHorizontal="true" or android:layout_centerVertical="true" in the child layout

    0 讨论(0)
提交回复
热议问题