源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android开发自定义TextView省略号样式的方法

  • 时间:2021-02-24 23:37 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android开发自定义TextView省略号样式的方法
本文实例讲述了Android开发自定义TextView省略号样式的方法。分享给大家供大家参考,具体如下: 在布局xml中设置textView的字段 [code]android:maxLines="2"[/code]  [code]android:ellipsize="end"[/code]字段之后,textview会自动压缩行数,并且对压缩掉的部分用...显示。如果不想用...而想用。。。或者... ...就需要自定义这个省略号的样式,不需要自定义控件,方法如下。 首先是布局文件
<TextView
    android:id="@+id/textView4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:maxLines="2"
    android:ellipsize="end"
    android:text="This is a text of TextView This is a text of TextView This is a text of TextView This is a text of TextView This is a text of TextView"
    android:textSize="20sp" />

对textView进行代码控制
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final TextView textView = (TextView)findViewById(R.id.textView4);
    Layout layout = textView.getLayout();//此时的layout为null,必须用观察者模式的回调函数来获取layout
    System.out.println("layout是"+layout);//此处为null
    ViewTreeObserver observer = textView.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
      boolean isfirstRunning = true;
      @Override
      public void onGlobalLayout() {//此方法会被调用两次,每执行一次settext就会执行一次,第一次是显示全部的文本,不加省略,第二次是加上省略,将会删减两次文本
        // TODO Auto-generated method stub
        if(isfirstRunning==false)return;//如果不加这一行的条件,出来之后是完整单词+。。。,如果加上,出来就是截断的单词+。。。
        Layout layout2 = textView.getLayout();
        System.out.println("layout2是"+layout2);
        if(textView!=null&&layout2!=null){
          int lines = layout2.getLineCount();
          System.out.println("当前行数是"+layout2.getLineCount());
          System.out.println("被省略的字符数量是"+layout2.getEllipsisCount(lines-1));//看看最后一行被省略掉了多少
          System.out.println("被省略的字符起始位置是"+layout2.getEllipsisStart(lines-1));//看看最后一行被省略的起始位置
          System.out.println("最后一个可见字符的偏移是"+layout2.getLineVisibleEnd(lines-1));
          //开始替换系统省略号
          if(lines<2)return;//如果只有一行,就不管了
          if(layout2.getEllipsisCount(lines-1)==0)return;//如果被省略的字符数量为0,就不管了
          String showText = textView.getText().toString();
          System.out.println("删减前"+showText);
          showText = showText.substring(0, layout2.getLineVisibleEnd(lines-1)-6).concat("。。。");//在此处自定义要显示的字符
          System.out.println("删减后"+showText);
          textView.setText(showText);
          isfirstRunning=false;
        }
      }
    });

更多关于Android相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/410.htm]Android开发入门与进阶教程[/url]》、《[url=http://www.1sucai.cn/Special/508.htm]Android调试技巧与常见问题解决方法汇总[/url]》、《[url=http://www.1sucai.cn/Special/381.htm]Android基本组件用法总结[/url]》、《[url=http://www.1sucai.cn/Special/375.htm]Android视图View技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/371.htm]Android布局layout技巧总结[/url]》及《[url=http://www.1sucai.cn/Special/124.htm]Android控件用法总结[/url]》 希望本文所述对大家Android程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部