简体   繁体   中英

Relativelayout align top and bottom

I would like to align center horizontial & center vertical all 3 buttons as 1 button in my relativelayout!

I used LinearLayout with parameter android:layout_weight, but Buttons's height is modified. This is my layout file. Thanks!

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Main" >

<Button
    android:id="@+id/bt_singleColor"
    android:layout_width="150dp"
    android:layout_height="100dp"
    android:layout_centerHorizontal="true"
    android:layout_margin="@dimen/button_margin"
    android:background="@drawable/button_circle"
    android:padding="@dimen/button_padding"
    android:text="@string/button_01"
    android:textColor="@color/button_text_color" />

<Button
    android:id="@+id/bt_shake"
    android:layout_width="150dp"
    android:layout_height="100dp"
    android:layout_below="@+id/bt_singleColor"
    android:layout_centerHorizontal="true"
    android:layout_margin="@dimen/button_margin"
    android:background="@drawable/button_circle"
    android:padding="@dimen/button_padding"
    android:text="@string/button_02"
    android:textColor="@color/button_text_color" />

<Button
    android:id="@+id/bt_autoColor"
    android:layout_width="150dp"
    android:layout_height="100dp"
    android:layout_below="@+id/bt_shake"
    android:layout_centerHorizontal="true"
    android:layout_margin="@dimen/button_margin"
    android:background="@drawable/button_circle"
    android:padding="@dimen/button_padding"
    android:text="@string/button_03"
    android:textColor="@color/button_text_color" />

You can use something like

<RelativeLayout to match parent or fill parent>
<LinearLayout with wrap_content and layout centerInParent>
<Button/>
<Button/>
<Button/>
</LinearLayout>
</RelativeLayout>

meaning that the LinearLayout is centered absolutely in the Relative layout and you can play with this alignment if you want something else. While for the Linearlayout you have wrap_content to detect its size based on the buttons size, margin and padding. Also don't forget about orientation for LinearLayout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM