«

android 带箭头的textview文字伸缩效果

时间:2024-3-2 18:41     作者:韩俊     分类: Android


虽然是自己做的 还是不太懂 都是看网上的方法自己总结的 如果大家有质量好的demo 请分享一下

主要就是一个方法 几个变量限制 xml文件就不贴了 普通的TextView

public class TextViewActivity extends Activity {

    private TextView tv;

    private String status = "up";
    private String str = "Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。";
    private String str_show = "";
    private int maxlength = 50;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.textView1);
        if (str.length() < maxlength) {
            tv.setText(str);
        } else {
            changStatus();
        }
    }

    private void changStatus() {
        tv.setText("");
        Drawable drawable = null;
        if (status.equals("down")) {
            drawable = getResources().getDrawable(R.drawable.icon_arrow_up);
            status = "up";
            str_show = str;
            tv.setMaxLines(10000);
        } else if (status.equals("up")) {
            drawable = getResources().getDrawable(R.drawable.icon_arrow_down);
            status = "down";
            tv.setMaxLines(2);
            str_show = str.substring(0, maxlength);
            str_show += "...";
        }
        String spanString = str_show;
        spanString = spanString.substring(0, spanString.length());
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight());
        SpannableString spannable = new SpannableString(spanString.toString()
                + "s");
        ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
        spannable.setSpan(span, spanString.toString().length(), spanString
                .toString().length() + "s".length(),
                Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        ClickableSpan rightClickableSpan = new ClickableSpan() {

            @Override
            public void onClick(View view) {
                changStatus();
            }
        };
        spannable.setSpan(rightClickableSpan, spanString.toString().length(),
                spanString.toString().length() + "s".length(),
                Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        tv.setText(spannable);
        tv.setMovementMethod(new MyTouch());

    }

}

标签: android

热门推荐