android - TextView.setMaxLines 不起作用?

标签 android android-layout

在我的应用程序中,我有一个屏幕,其中显示一些文本,然后显示一张照片。文本的长度是可变的(有时根本没有,有时很多),所以我想对其进行设置,以便文本不会占用超过几行(但可以滚动),从而为下面的图像留出足够的空间。

我的这部分 View 组件是通过编程方式创建的,我调整了代码以具有以下内容(目前在我的文本设置方法中,但如果它在初始 View 创建代码中也会发生同样的事情)

public void SetDescription(String description)
{
    mTxtDescription.setText(Html.fromHtml(description));
    mTxtDescription.setClickable(true);
    mTxtDescription.setMaxLines(5);
    mTxtDescription.setLines(5); //this makes no difference either!
    mTxtDescription.setSingleLine(false);
    mTxtDescription.setScrollbarFadingEnabled(true);
    mTxtDescription.setScrollBarStyle(VERTICAL);
    mTxtDescription.setMovementMethod(ScrollingMovementMethod.getInstance());
    mTxtDescription.invalidate(); //adding this made no difference...
}

但是它不起作用 - 长文本仍然填满整个屏幕,并且图像由于被向下推到 0 的高度而消失了。我怎样才能让文本永远不超过 5 行?

最佳答案

尝试删除对 setSingleLine 的调用。并使用 setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE)。它还会将此调用放在 setMaxLinessetLines 调用之前以确保。

注意:setLines 覆盖了 setMaxLinessetMinLines 的设置。

TextView 围绕如何显示多个、省略号等的各种调用有很多问题。

关于android - TextView.setMaxLines 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11408922/

相关文章:

android - 在 LinearLayout 中嵌套 RelativeLayouts

android - 制作 :hover on Android require two clicks to open a link

android - 如何编写扩展函数来在 Kotlin 中实例化 AndroidViewModel?

java - 获取 listPreference 选定的项目。安卓

Android解码器->解码为位图下载返回false

android - gridlayout 2列中心水平

android - setLayerType 替代Android 2.3.3?

android - 使用 WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY 将 View 添加到窗口时,它没有获取触摸事件

android - Android xml 形状声明中的滑稽错误

android - Xamarin for Android 与 SQL Server 2008