首页 > 解决方案 > TextView 中的偏移文本

问题描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="90px"
    android:layout_height="20px">
    <TextView
        android:maxLines="1"
        android:ellipsize="end"
        android:textSize="15px"
        android:textColor="@android:color/white"
        android:background="@color/material_grey_600"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:text="hello this is some long text" />
</LinearLayout>

此代码成功地垂直对齐文本视图中的文本。字体大小为视图高度的 75%。

在此处输入图像描述

但是,如果我将文本的大小增加到视图高度的 19 像素或 95%,则垂直居中会中断,因为文本视图似乎不会将文本绘制到高于视图顶部的位置.

在此处输入图像描述

是否可以将文本分流?

标签: android

解决方案


您可以设置高度,wrap_content然后用于lineSpacingMultiplier将行高设置为1.3文本高度的倍数。这意味着文本总是大约是行高的 75%。

此外,您应该使用sp而不是dp文本大小,以便当人们在他们的设置中更改文本大小时,您的应用程序中的文本大小会更新,这对于可访问性很重要。

最后,除非您打算添加更多视图,否则您不需要将您TextViewLinearLayout


推荐阅读