将按钮的文本对齐到底部

Align the text of a button to the bottom

我遇到了问题。
我想用 20dp 的填充将按钮的文本对齐到底部。 但是,文本停留在我的按钮底部。

<Button
    android:id="@+id/CatButton"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:paddingBottom="20dp"
    android:background="@drawable/rounded_1_selected"
    android:gravity="bottom|center_horizontal"
    android:text="test"/>

谁能帮帮我?

因为你设置了android:gravity="bottom|center_horizontal".

如果你想让它的文字居中显示,你应该设置android:gravity="center"

--编辑--

这是一个错误。我误会了。

你应该设置android:gravity="center|bottom".

试试这个

<Button
        android:id="@+id/CatButton"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:paddingBottom="20dp"
        android:gravity="center_vertical|center_horizontal"
        android:text="test"/>

如果你改变layout_gravity = "bottom",你的按钮对齐底部;但是如果你改变 gravity = "bottom",你的按钮的文本对齐底部。你应该这样做:

<Button
            android:id="@+id/catButton"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_gravity="center" 
            android:text="test"
            android:gravity="center|bottom"/>

我们可以借助gravity属性来实现它。

android:gravity="start|bottom"

例如:

<Button
    android:id="@+id/btn_submit"
    android:layout_width="100dp"
    android:layout_height="150dp"
    android:layout_gravity="center" 
    android:text="submit"
    android:gravity="start|bottom"/>