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);
    }
  • 相关阅读:
    html5 canvas 渐变
    html5 canvas 画直线
    html5在canvas中插入图片
    Window文件夹右击菜单定制
    HTML中解决双击会选中文本的问题
    Linux 下修改mysql 字符集编码
    mysqlimport导入命令使用
    PAM 2500 荧光数据导出数据整合脚本
    Resources for Ecology and Evolution
    Plant Ecology Journal Club, 分享主题和文献列表 825, 2019年春
  • 原文地址:https://www.cnblogs.com/zijianlu/p/4876826.html
Copyright © 2011-2022 走看看