按钮混合中的回车带有边距

问题描述 投票:0回答:2

如果我在一个按钮中添加回车符,则会在顶部显示额外的“余量”(缺少更好的解释):

<?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>

enter image description here

如果我不添加回车符,则它完全对齐:enter image description here

如何阻止额外的“保证金”出现?我尝试设置android:lines="2"和/或android:singleline="false",但似乎没有任何帮助。

android android-layout button line-breaks
2个回答
1
投票

你需要在你的android:gravity="center"中设置LinearLayout

示例代码

<?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>

0
投票

无需添加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>
© www.soinside.com 2019 - 2024. All rights reserved.