zoukankan      html  css  js  c++  java
  • 获取textview行数

    获取textview行数

    代码

    import android.content.Context;
    import android.graphics.Canvas;
    import android.text.Layout;
    import android.text.Layout.Alignment;
    import android.text.StaticLayout;
    import android.text.TextUtils;
    import android.util.AttributeSet;
    import android.widget.TextView;
    
    
    /**
     * @author jasonkent27
     *
     * TextView第二行文字超过一半则自动略去,并添加省略号
     */
    public class CustomWidgetTextView extends TextView {
    
        private float mLineSpacingMultiplier = 1.0f;
        private float mLineAdditionalVerticalPadding = 0.0f;
        /**
         * setText时需要置为true
         */
        private boolean mNeedResetText = true ;
    
        public CustomWidgetTextView(Context context) {
            this(context, null);
        }
    
        public CustomWidgetTextView(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public CustomWidgetTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected final void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
            mNeedResetText = true ;
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            if (mNeedResetText) {
                resetText();
                mNeedResetText = false ;
            }
            super.onDraw(canvas);
        }
    
        /**
         * 重TextView内部文字渲染逻辑
         */
        private void resetText() {
    
            if (!TextUtils.isEmpty(getText())) {
    
                String origText = getText().toString();
                String firstLineText ;
                String secondLineText ;
                String resultText = origText ;
    
                Layout layout = createRenderLayout(origText, getWidth() - getPaddingLeft() - getPaddingRight());
                if (layout.getLineCount() > 1) {
                    //取出第一,二行文字
                    firstLineText = origText.substring(0, layout.getLineEnd(0));
                    secondLineText = origText.substring(layout.getLineEnd(0)+1, layout.getLineEnd(1));
    
                    Layout layout2 = createRenderLayout(secondLineText, (getWidth() - getPaddingLeft() - getPaddingRight()) / 2);
                    //第二行文字长度过半,则截断并且添加省略符
                    if (layout2.getLineCount() > 1) {
                        secondLineText = secondLineText.substring(0, layout2.getLineEnd(0)) + "...";
                    }
                    resultText = firstLineText + secondLineText ;
                }
                setText(resultText);
            }
        }
    
        /**
         * @param workingText
         * @param width
         * @return StaticLayout @See https://developer.android.com/reference/android/text/StaticLayout.html
         */
        private Layout createRenderLayout(CharSequence workingText, int width) {
            return new StaticLayout(
                    workingText,
                    getPaint(),
                    width,
                    Alignment.ALIGN_NORMAL,
                    mLineSpacingMultiplier,
                    mLineAdditionalVerticalPadding,
                    false );
        }
    }
    
  • 相关阅读:
    构建maven项目,自定义目录结构方法
    Nginx反向代理实现负载均衡以及session共享
    Spring Boot 2.x引入JS,CSS 失效问题
    WebMvcConfigurerAdapter已过时
    闲谈Tomcat性能优化
    oracle decode函数和 sign函数
    为什么要使用MQ和到底什么时候要使用MQ
    redis持久化的几种方式
    【mySQL】left join、right join和join的区别
    redis缓存在项目中的使用
  • 原文地址:https://www.cnblogs.com/jasonkent27/p/5642790.html
Copyright © 2011-2022 走看看