如何限制Android中TextView文本的行数?

时间:2014-07-17 07:50:48

标签: android

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:weightSum="3" >

<TextView
    android:id="@+id/info"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:gravity="bottom|center"
    android:maxLines="10"
    android:scrollbars="vertical"
    android:text="@string/hello_world"
    android:textSize="20sp" />

<SeekBar
    android:id="@+id/seek_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

我设置了一个TextView和一个搜索栏,然后设置了搜索栏的监听器,每次值变化时都会在textview中附加一行文本,因为textview会得到越来越多的文本,应用程序完全变慢,我试着使用maxlines或ellipsize之类的东西来限制,截断文本,但似乎无法正常工作。

我可以使用哪些内容构建?

这是java代码:

this.seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
            info.append("\n" + "Stop" + "Current: " + seekBar.getProgress());
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
            info.append("\n" + "Start" + "Current: " + seekBar.getProgress());
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            // TODO Auto-generated method stub
            info.append("\n" + "Changing" + "Current: " + seekBar.getProgress());
        }
    });

3 个答案:

答案 0 :(得分:0)

您使用重量“1”的事实可能与您想要的固定尺寸相冲突。我建议尝试使用“wrap_content”作为高度值。

希望它能解决你的问题。

修改:

点击此链接,尝试实施此人提供的解决方案:D
将textview替换为讨论中的文本视图。

android ellipsize multiline textview

这似乎就是你要找的。

答案 1 :(得分:0)

    final int maxLength = 50;
    String mLongText = "I am very long sentence, but allowed maximum length is 50, trim me if I am more than 50chars";
    if(mLongText!="" && mLongText.length() > maxLength)
    {
        mLongText = mLongText.substring(0, maxLength);
    }
    mTextView.setText(mLongText);

修改

您可以按 mTextView.setMaxLines(10);设置最大行数。这将显示最多 10 行。在xml android:maxLines="10"

您使用了android:ellipsize="end",这意味着最后会附加...

EX:ABCDEFGHIJKLMNOPQRSTUVWXYZ将显示为ABCDEFGHIJ...

android:ellipsize="start"将显示为...QRSTUVWXYZ

Refer this to get more info

答案 2 :(得分:0)

此示例将文本剪切为最多3行

final TextView title = (TextView)findViewById(R.id.text);
title.setText("A really long text");
ViewTreeObserver vto = title.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

    @Override
    public void onGlobalLayout() {
        ViewTreeObserver obs = title.getViewTreeObserver();
        obs.removeGlobalOnLayoutListener(this);
        if(title.getLineCount() > 3){
            Log.d("","Line["+title.getLineCount()+"]"+title.getText());
            int lineEndIndex = title.getLayout().getLineEnd(2);
            String text = title.getText().subSequence(0, lineEndIndex-3)+"...";
            title.setText(text);
            Log.d("","NewText:"+text);
        }

    }
});