Button 中的回车符 return 弄乱了边距

Carriage return in Button messes with the margin

如果我在按钮中添加回车符 return,顶部会出现额外的 "margin"(由于缺乏更好的解释):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff992a2a">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="200dp">
        <Button
            android:layout_width="250dp"
            android:layout_height="100dp"
            android:text="Extend" />
        <Button
            android:layout_width="250dp"
            android:layout_height="100dp"
            android:text="Accept
0:23"/>
    </LinearLayout>
</RelativeLayout>

如果我不添加回车 return,它会完美对齐:

我怎样才能阻止额外的 'margin' 出现?我尝试设置 android:lines="2" 和 and/or android:singleline="false" 但似乎没有任何帮助。

您需要在 LinearLayout

中设置 android:gravity="center"

示例代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff992a2a">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:layout_width="250dp"
            android:layout_height="100dp"
            android:text="Extend" />

        <Button
            android:layout_width="250dp"
            android:layout_height="100dp"
            android:text="Accept
0:23" />
    </LinearLayout>
</RelativeLayout>

无需添加LinearLayout。您可以只使用 RelativeLayout。 试试这个

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff992a2a">

    <Button
        android:id="@+id/button_1"
        android:layout_width="250dp"
        android:layout_height="100dp"
        android:text="Extend" />

    <Button
        android:id="@+id/button_2"
        android:layout_width="250dp"
        android:layout_height="100dp"
        android:layout_toRightOf="@id/button_1"
        android:text="Accept
            0:23" />

</RelativeLayout>