文字长度可变跑马灯效果

来源:互联网 发布:58同城网络兼职日结 编辑:程序博客网 时间:2024/06/05 15:37

  

布局文件属性设置

<TextView

        android:id="@+id/tv_text"
        android:layout_width="90dp"
        android:layout_height="wrap_content"

        android:ellipsize="marquee"

        android:marqueeRepeatLimit="marquee_forever"

        android:focusable="true"
        android:focusableInTouchMode="true"

        android:singleLine="true" />


运行时代码

int maxWidth = getResources().getDisplayMetrics().widthPixels;

       TextView textView = (TextView) findViewById(R.id.tv_text);


public void setData(String data){

        float textLengthPx = textView.getPaint().measureText(data);
        if(maxWidth >= textLengthPx){
            data += "\t\t";
            textView.getLayoutParams().width = (int) (textLengthPx);
        }
        textView.setText(data);
        textView.requestFocus();
    }


原创粉丝点击