【问题标题】:How to limit the number of lines of TextView's text in Android?Android中如何限制TextView的文本行数?
【发布时间】:2014-09-07 23:36:34
【问题描述】:
<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和一个seekbar,然后设置seekbar的监听器在每次值更改时将一行文本附加到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());
        }
    });

【问题讨论】:

  • 可能你的字符串太长了。尝试设置字符串的长度可能会有所帮助,优点是加载有限长度所需的时间更少。
  • 谢谢,你的意思是太长了?是的,我同意,我想找到 android 内置的东西来限制它。
  • 太长意味着长度很大。设置最大行数的正常方法是 android:maxLines="10"。那就是你正在使用。如果它不起作用,那么尝试设置字符串的限制。我只是给你评论,这不是一个完美的解决方案。詹克斯。
  • 为什么不在 seekbar 监听器中检查文本的长度,如果已经太长,那么就不要附加文本?
  • @Amsheer android:maxLines 只是限制显示框的大小,而不是字符串包含的长度。对于EditTextandroid:maxLength 限制文本的长度。但由于他/她使用的是TextView 而不是EditText,我认为他/她需要在搜索栏侦听器中以编程方式限制长度。

标签: android


【解决方案1】:

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

希望它能解决你的问题。

编辑:

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

android ellipsize multiline textview

这似乎就是你要找的东西。

【讨论】:

  • 你能附加整个xml吗?
  • 好的,我现在穿上它们
  • 似乎不需要 weight_sum。您是否尝试过以编程方式进行操作?
  • 不幸的是,这似乎是一个错误,您无法限制数量。为什么不尝试使用这个人提供的解决方案? stackoverflow.com/questions/2160619/…。你可以用他的课。社区正在确认该代码是可行的解决方案。 :D
【解决方案2】:

此示例将您的文本最多剪切为 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);
        }

    }
});

【讨论】:

    【解决方案3】:
        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

    【讨论】:

    • 我想要限制线...而不是长度如何?
    • 我希望 textview 中的文本只有 10 行左右,前面的行会被主干。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    相关资源
    最近更新 更多