TextView杂知识

来源:互联网 发布:室内设计装修软件 编辑:程序博客网 时间:2024/06/10 15:26

第一:获取 textview判断是否有省略号

方法一:getlineCount()  返回值为0

Layout l = location.getLayout();
         if ( l!= null){
             int lines = location.getLineCount();
             Log.v(TAG, "location有几行"+lines);
             if ( lines > 1)
              contentLayout.setOrientation(LinearLayout. VERTICAL);
         }   
         

方法二:可用,但是textview有省略号,getlineCount() 也是返回 1


         location.post(new Runnable() {
            @Override
            public void run() {
             Log.v(TAG,"当前"+location.getLineCount());
            }
         });
        

方法三:可用,textview有省略号,getlineCount() 也是返回 1;l.getEllipsisCount(lines-1) > 0 可以判断是否有省略号


         ViewTreeObserver vto = location.getViewTreeObserver();

         vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

             @Override

             public void onGlobalLayout() {

                Layout l = location.getLayout();

                if ( l != null){

                   int lines = l.getLineCount();
                   Log.v(TAG, "最后一个方法location有几行"+lines);
                   if ( lines > 0)

                       if ( l.getEllipsisCount(lines-1) > 0)

                   /* 判断有省略号后的动作*/

                   contentLayout.setOrientation(LinearLayout. VERTICAL);

                } 

             }

         });

第二:代码动态改变文字颜色

textview.setTextColor(this.getResources().getColor(R.color.white));

第三:代码改变TextView背景颜色

qianyan_layout.setBackgroundResource(R.color.tab_top_bg);
0 0
原创粉丝点击