TextView.setMaxLines无法正常工作? [英] TextView.setMaxLines not working?

查看:72
本文介绍了TextView.setMaxLines无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my app I have a screen where I display some text and then a photo. The text is variable in length (sometimes none at all, sometimes a lot), so I wanted to have it set up so the text never takes up more than a few lines (but can be scrolled) leaving enough room for the image below.

我这部分的视图组件是通过编程方式创建的,并且我已经对代码进行了调整,使其具有以下功能(当前是在我的文本设置方法中,但是如果在初始的视图创建代码中也会发生同样的事情)

My view component for this part is created programatically, and I've adjusted the code to have the following (currently in my text-setting method, but the same thing happens if it's in the initial view-create code)

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行?

However it doesn't work- long text still fills the whole screen and the image has vanished due to being pushed down to a height of 0. How can I get the text to never be more than 5 lines?

推荐答案

尝试删除对 setSingleLine 的调用.并使用 setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE).还要确保将此调用放在 setMaxLines setLines 调用之前.

Try removing the call to setSingleLine. And use setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE). It'd also put this call before the setMaxLines and setLines call to be sure.

注意: setLines 会覆盖 setMaxLines setMinLines 的设置.

Note: setLines overrides the settings of setMaxLines and setMinLines.

TextView 在围绕如何显示多个,省略号等各种调用方面存在许多问题.

The TextView has many issues surrounding the various calls to how it should display multiple, ellipses, etc.

这篇关于TextView.setMaxLines无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆