Android TextView:我可以停止出现部分显示的文本

时间:2013-03-27 11:21:07

标签: android user-interface text textview xamarin.android

在我的应用程序中,我显示了几个文本视图,其中包含在运行时加载的各种长度的文本。在运行时之前,我不知道文本视图的尺寸或文本的长度。有时,当文本很长并且textview很小时,一些文本部分可见,例如:

enter image description here

我想删除部分可见的文本,因为它看起来有点笨拙,但我找不到办法做到这一点。任何帮助将不胜感激!

谢谢,

戴夫

3 个答案:

答案 0 :(得分:0)

您可以以第二行文字不可见的方式对TextView高度进行硬编码。

或使用:

android:maxLines , Makes the TextView be at most this many lines tall. 

如上所述。

答案 1 :(得分:0)

将您的文本视图放在滚动视图布局中。并为文本视图指定特定的宽度,并使高度换行内容。这样您的文本就不会被剪切。

答案 2 :(得分:0)

这就是我做的。我通过将方法CheckTextIsVisible发布到父relativelayout的处理程序队列来加载活动后运行此代码,否则将不知道textviews的高度:

m_eventsLayout.Post(new Action(CheckTextIsVisible));

然后CheckTextIsVisible方法找到每个文本视图,其中包含文本,计算字体的高度,计算出文本视图中可以容纳的行数,并相应地设置最大行数:

    private void CheckTextIsVisible()
    {
        View view;
        TextView tView;
        Android.Text.TextPaint tPaint;
        float height;
        int heightOfTextView;
        int noLinesInTextView;
        for (int i = 0; i < m_eventsLayout.ChildCount; i++)
        {
            view = m_eventsLayout.GetChildAt(i);

            if (view is TextView)
            {
                tView = (TextView)view;
                if (tView.Text != "")
                {         
                    //calculate font height
                    tPaint = tView.Paint;
                    height = CalculateTextHeight(tPaint.GetFontMetrics());
                    //calculate the no of lines that will fit in the text box based on this height
                    heightOfTextView = tView.Height;
                    noLinesInTextView = (int)(heightOfTextView / height);
                    //set max lines to this
                    tView.SetMaxLines(noLinesInTextView);
                }
            }
        }
    }

    private float CalculateTextHeight(Android.Graphics.Paint.FontMetrics fm)
    {
        return fm.Bottom - fm.Top;
    }

这导致没有部分可见的文字!