简体   繁体   English

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

[英]Can you center a Button in RelativeLayout?

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.我试过重力和方向功能,但它们什么也没做。

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>

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 RelativeLayoutandroid:layout_centerInParent="true"添加到要在 RelativeLayout 中居中的元素

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.设置 RelativeLayout 本身的重力将影响放置在其中的所有对象。 In this case, it's just the button.在这种情况下,它只是按钮。 You can use any of the gravity settings here, of course (eg center_horizontal, top, right, and so on).当然,您可以在此处使用任何重力设置(例如 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>

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"

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>

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

在子布局中使用 android:layout_centerHorizo​​ntal="true" 或 android:layout_centerVertical="true" 居中对齐方向之一

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>

It's pretty simple, just use Android:CenterInParent="true" to center both in horizontal and vertical, or use Android:Horizontal="true" to center in horizontal and Android:Vertical="true"这很简单,只需使用Android:CenterInParent="true"以水平和垂直居中,或使用Android:Horizontal="true"以水平居中和Android:Vertical="true"

And make sure you write all this in RelaytiveLayout并确保您在 RelaytiveLayout 中编写所有这些

Removing any alignment like android:layout_alignParentStart="true" and adding centerInParent worked for me.删除任何对齐方式,例如 android:layout_alignParentStart="true" 并添加 centerInParent 对我有用。 If the "align" stays in, the centerInParent doesn't work如果“对齐”保持不变,则 centerInParent 不起作用

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_alignRight=""

etc.等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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