首页 > 解决方案 > 当 textSize 超过 maxHeight 时,按钮文本不居中

问题描述

我正在尝试将按钮内的一些文本垂直居中,但是当 textSize 超过按钮的高度时,文本会与顶部对齐,就像 android:gravity="top" 而不是 android:gravity="center"

我通过添加 android:includeFontPadding="false" 和 android:paddingTop="0dp" 稍微改进了结果,但文本没有居中。我希望文本垂直居中,因此在按钮上方和下方被剪辑。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
    android:id="@+id/button3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:includeFontPadding="false"
    android:maxHeight="40dp"
    android:paddingTop="0dp"
    android:text="Button"
    android:textSize="60dp" />
</android.support.constraint.ConstraintLayout>

这是布局的屏幕截图...

https://imgur.com/gallery/8I5IRZn

标签: androidxmlandroid-layout

解决方案


尝试使用负填充或边距来向上移动文本。


推荐阅读