Android 你可以在RelativeLayout中将按钮居中吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3748255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:38:28  来源:igfitidea点击:

Can you center a Button in RelativeLayout?

androidlayout

提问by Arcadia

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.

我试图在相对布局中将按钮居中,这可能吗?我试过重力和方向功能,但它们什么也没做。

回答by ShadowGod

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>

回答by Josnidhin

You can use CENTER_IN_PARENT for relative layout.

您可以使用 CENTER_IN_PARENT 进行相对布局。

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

添加android:layout_centerInParent="true"到要在相对布局中居中的元素

回答by Kevin Coppock

Arcadia, just try the code below. There are several ways to get the result you're looking for, this is one of the easier ways.

阿卡迪亚,试试下面的代码。有几种方法可以获得您正在寻找的结果,这是一种更简单的方法。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relative_layout"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:gravity="center">

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

Setting the gravity of the RelativeLayout itself will affect all of the objects placed inside of it. In this case, it's just the button. You can use any of the gravity settings here, of course (e.g. center_horizontal, top, right, and so on).

设置 RelativeLayout 本身的重力将影响放置在其中的所有对象。在这种情况下,它只是按钮。当然,您可以在此处使用任何重力设置(例如 center_horizo​​ntal、顶部、右侧等)。

You could also use this code below:

您也可以在下面使用此代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relative_layout"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">

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

回答by Spektakulatius

Summarized

总结

Adding:

添加:

android:layout_centerInParent="true"

just works on RelativeLayout, if one of the following attributes is also set for the view:

如果还为视图设置了以下属性之一,则仅适用于相对布局:

android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"

which alignes the child view to the parent view. The "center" is based on the axis of the alignement you have chosen:

它将子视图与父视图对齐。“中心”基于您选择的对齐轴:

left/right -> vertical

左/右 -> 垂直

top/bottom -> horizontal

顶部/底部 -> 水平

Setting the gravity of childs/content inside the view:

在视图中设置子项/内容的重力:

android:gravity="center"

is centering the child inside the parent view in any case, if there is no alignment set. Optional you can choose:

如果没有对齐设置,则无论如何都将子视图置于父视图内。可选您可以选择:

<!-- Axis to Center -->
android:gravity="center_horizontal"
android:gravity="center_vertical"

<!-- Alignment to set-->
android:gravity="top"
android:gravity="bottom"
android:gravity="left"
android:gravity="right"
android:gravity="fill"
...

Then there is:

然后是:

android:layout_gravity="center"

which is centering the view itself inside it's parent

这是将视图本身置于其父级内部的中心

And at last you can add the following attribute to the parents view:

最后,您可以将以下属性添加到父母视图中:

android:layout_centerHorizontal="true"
android:layout_centerVertical="true"

回答by KarthikKPN

Use the attribute android:centerInParent="true"inside a view , when you want to center the view in Relative layout.

android:centerInParent="true"当您希望在相对布局中将视图居中时,请在视图中使用该属性。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TextView
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="NWE"
        android:textSize="30dp"/>
</RelativeLayout>

回答by Sheychan

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"/>

回答by karma

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

要在一个方向上居中对齐,请在子布局中使用 android:layout_centerHorizo​​ntal="true" 或 android:layout_centerVertical="true"

回答by al881

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

删除任何对齐方式,例如 android:layout_alignParentStart="true" 并添加 centerInParent 对我有用。如果“对齐”保持不变,则 centerInParent 不起作用

回答by Primesh

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>

回答by Divy sindhu

you must have aligned it to left or right of parent view

您必须将其与父视图的左侧或右侧对齐

don't use any of these when using android:centerInParent="true"codes:-

使用android:centerInParent="true"代码时不要使用任何这些 :-

android:layout_alignParentLeft="true"

android:layout_alignParentLeft="true"

android:layout_alignParentLeft="true"

android:layout_alignParentLeft="true"

android:layout_alignRight=""

android:layout_alignRight=""

etc.

等等。