简体   繁体   中英

How to center a button at the bottom of a FrameLayout in a RelativeLayout

This is my xml code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/nero"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="vertical">

        <Button
            android:layout_width="130dp"
            android:layout_height="130dp" />

         ....

    </RelativeLayout>

    <FrameLayout
        android:id="@+id/imprint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal">

        <Button
            android:layout_width="230dp"
            android:layout_height="230dp" />
    </FrameLayout>

</LinearLayout>

I want to place the FrameLayout at the bottom of the LinearLayout and all elements in the FrameLayout should be centered. How can I do that?

I don't know exactly if that would solve the issue, but you can use "wrap_content" as your layout_width in your FrameLayout at the bottom. That would center the FrameLayout, so the views which are in the layout would get centered.

To center the button inside the FrameLayout you should add:

android:layout_gravity="center"

inside the Button or:

android:gravity="center"

inside the FrameLayout . And btw, if a Button is the only child why are you using a FrameLayout ?

In Frame Layout Use Layout_Gravity will work

 android:layout_gravity="bottom|center"

Also Use Bottom margin to make it in perfect location from bottom.

android:layout_marginBottom="20dp"

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