TextView.SetMaxLines填充文本n行

时间:2017-03-03 18:08:40

标签: android textview xamarin.android

我一直在尝试创建一个可扩展的TextView,以5行开头(如果初始文本超过5行)。

我到目前为止的代码如下:

TextView
    textView;
Button
    button;

textView.LayoutChange += delegate {
    if( textView.LineCount > 5 ) {
        button.Visibility = ViewStates.Visible;
        textView.SetMaxLines( 5 );

        button.Click += delegate {
            button.Visibility = ViewStates.Gone;
            textView.SetMaxLines( Int32.MaxValue );
        };
    }
};

textView.Text = "Text that may occupy more than 5 lines due to size.";

基本上,我有TextView我在delegate事件中添加了LayoutChange。之后,我将初始文本设置为textView。到现在为止还挺好。设置文本后会调用delegate,如果文字跨越行数限制,则会将MaxLines的{​​{1}}设置为textView

问题是,当用户点击5时,button textView设置为MaxLines到目前为止,非常好)但文本从顶部填充 5行,剪切文本。

我尝试将Int32.MaxValue设置为0,设置为MaxLines和/或将文本设置为Int32.MaxValue,然后再设置为所需文本,但没有结果。< / p>

知道发生了什么,或者我在这里做错了什么?

编辑 添加图片以说明正在发生的事情。文本似乎填充了与文本最初占用的空间相同的空间,并设置了""

左侧图片是MaxLinestextView设置为MaxLines,右侧是5textView设置为MaxLines }。

1 个答案:

答案 0 :(得分:0)

好的......对于这个问题,我可能找到了 解决方案 - 而不是 解决方案。它甚至可能是Xamarin或Android本身的错误。

无论哪种方式,我都发现TextView.SetMaxLines( Int32 )不喜欢突然的变化。因此,为了使这项工作在TextView无法填充填充的情况下,我必须设置while,在将值传递给方法之前将值递增1。

最终结果如下:

TextView
    textView = ...;
Button
    buttonView = ...;
Int32
    lineCount = 0,
    maxLines = 5;

textView.LayoutChange += delegate {
    if( textView.LineCount <= maxLines ) {
        return;
    }

    lineCount = textView.LineCount;

    buttonView.Visibility = ViewStates.Visible;
    textView.SetMaxLines( maxLines );

    buttonView.Click += delegate {
        buttonView.Visibility = ViewStates.Gone;

        while( maxLines++ <= lineCount ) {
            textView.SetMaxLines( maxLines );
        }
    };
};