zoukankan      html  css  js  c++  java
  • TextView 获取行数,某一行的内容,某行的宽度

    获取行数

    ViewTreeObserver vto = textView.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    
        @Override
        public void onGlobalLayout() {
            ViewTreeObserver obs = textView.getViewTreeObserver();
            obs.removeGlobalOnLayoutListener(this);
            int lineCount = textview.getLineCount(); //行数
    
        }
    });
    
    //或者
    textview.setText(“Some text”);
    textview.post(new Runnable() {
        @Override
        public void run() {
            int lineCount = textview.getLineCount();//行数
        }
    });
    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);
                    }
    
                }
            });
    View Code

    获取行的内容和宽度

    Layout layout = edit.getLayout();
    String text = edit.getText().toString();
    int start = 0;
    int end;
    for (int i = 0; i < edit.getLineCount(); i++) {
        end = layout.getLineEnd(i);
                        
        String line = text.substring(start, end); //指定行的内容
        start = end;
        float width = layout.getLineWidth(i); //指定行的宽度
        
        Log.e("test", line + "," + width);
    }
  • 相关阅读:
    http 协议相关问题
    网卡中断及多队列
    Visual Studio Code 配置C/C++环境
    C++通用框架和库
    命令行的艺术
    NetScaler Logs Collection Guide
    C++性能榨汁机之无锁编程
    Codeforces 839E Mother of Dragons【__builtin_popcount()的使用】
    C/C++中__builtin_popcount()的使用及原理
    Codeforces 839D Winter is here【数学:容斥原理】
  • 原文地址:https://www.cnblogs.com/zijianlu/p/4876826.html
Copyright © 2011-2022 走看看