首页 > 解决方案 > TextView 的高度没有增加超过屏幕高度

问题描述

问题:在 android 中将文本添加到文本视图时,当文本足够长以使 textview 高度大于屏幕时,textview 只会将其高度增加到屏幕高度而不超出屏幕。

要求:根据文本内容增加文本视图的高度。是否可以在运行时更改文本视图的高度?

编辑 即使我在 XML 中更改了电视的高度(大于屏幕的高度),我的电视高度仍然没有超过屏幕高度。

编辑 2 我通过使用画布绘制 TextView 来实现这一点。我不确定这有多有效。

标签: androidtextview

解决方案


来自 MavenCentral 的 AutofitTextView 库很好地处理了这个问题。源代码托管在 Github(1k+ 星)上,位于https://github.com/grantland/android-autofittextview

应用程序/构建.Gradle

repositories {
    mavenCentral()
}

dependencies {
    compile 'me.grantland:autofittextview:0.2.+'
}

在代码中启用任何扩展 TextView 的视图:

AutofitHelper.create(textView);

在 XML 中启用任何扩展 TextView 的视图:

<me.grantland.widget.AutofitLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        />
</me.grantland.widget.AutofitLayout>

在代码或 XML 中使用内置的 Widget:

<me.grantland.widget.AutofitTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"
    />

推荐阅读