TextView 部分字体高亮

TextView 部分字体高亮

 

[功能]

TextView是不支持部分字段高亮的 但是我们可以进行扩展

 

 

[思路]

1. 利用LinearLayout 作为 TextView 的 容器

2. 字符串中每个字都使用一个TextView显示之

3. 还可以使用*.9.png来作为所有TextView的背景 使之看上去成为整体

 

 

[思路 步骤]

 

1. 定义TextSelectionHelper 构造函数 传入 Activity上下文 及 子View对齐方式 以及 layout_width layout_height

Java代码 
  1. public class TextHighlightHelper{  
  2.     Activity activity;  
  3.       
  4.     LinearLayout lLayout;  
  5.       
  6.     public TextHighlightHelper(Activity a,int l){  
  7.   activity = a;  
  8.     
  9.   lLayout = new LinearLayout(activity);  
  10.   lLayout.setOrientation(l);  
  11.   lLayout.setLayoutParams(  
  12.     new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));  
  13.  }  
  14. }  

 

 

2. 定义函数 用于接收字符串

Java代码 
  1. //之所以每个字符都分别用一个TextView显示之 因为这样做 会使得后面颜色设定非常容易  
  2.   
  3. public void addText(CharSequence cs){  
  4.   for(int i=0;i<cs.length();i++){  
  5.    TextView tv = new TextView(activity);  
  6.    tv.setText(cs.charAt(i)+"");  
  7.      
  8.    lLayout.addView(tv);  
  9.   }  
  10.  }  

 

 

3. 设定 部分字符 颜色

Java代码 
  1. //函数解释: 从s开始 选取l个字符 颜色都设定为i  
  2.   
  3. public void addColor(int s,int l,int c){  
  4.   if(l > lLayout.getChildCount()){  
  5.    //error argument  
  6.   }  
  7.   else {  
  8.    for(int i=s;i<s+l;i++){  
  9.     TextView item = (TextView)lLayout.getChildAt(i);  
  10.       
  11.     item.setTextColor(c);  
  12.    }  
  13.   }  
  14.  }  

 

4. 设定所有字符的背景 最好使用*.9.png 资源 因为长度可变

Java代码 
  1. public void addBackResource(int r){  
  2.   lLayout.setBackgroundResource(r);  
  3.  }  

 

 

5. 得到整个LinearLayout 并供使用

Java代码 
  1. public View loadView(){  
  2.         return lLayout;  
  3.     }  

 

 

6. 如何使用TextSelectionHelper

* TextHighlightUsage 的布局 并定义最外层的id

Java代码 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:id="@+id/layout"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent"  
  7.     >  
  8. <TextView   
  9.     android:text="HelloText1"  
  10.     android:layout_width="fill_parent"  
  11.     android:layout_height="wrap_content" />  
  12. </LinearLayout>  

 

 

* 具体使用:

Java代码 
  1. public class TextHighlightUsage extends Activity {  
  2.     TextHighlightHelper tHelper1;  
  3.     TextHighlightHelper tHelper2;  
  4.       
  5.     LinearLayout ll;  
  6.     /** Called when the activity is first created. */  
  7.     @Override  
  8.     public void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.main);  
  11.           
  12.         ll = (LinearLayout)findViewById(R.id.layout);  
  13.           
  14.         //Text:HelloText2  
  15.         CharSequence c1 = "HelloText2";  
  16.         tHelper1 = new TextHighlightHelper(this,LinearLayout.HORIZONTAL);  
  17.         tHelper1.addText(c1);  
  18.         tHelper1.addColor(03, Color.RED);  
  19.         tHelper1.addBackResource(R.drawable.dot);  
  20.           
  21.         ll.addView(tHelper1.loadView());  
  22.           
  23.         //Text:创新源于模仿!  
  24.         CharSequence c2 = "创新源于模仿!";  
  25.         tHelper2 = new TextHighlightHelper(this,LinearLayout.VERTICAL);  
  26.         tHelper2.addText(c2);  
  27.         tHelper2.addColor(13, Color.RED);  
  28.           
  29.         ll.addView(tHelper2.loadView());  
  30.     }  
  31. }  

 

 

7. emulator 运行截图:

 

 

至于其能不能满足需求 见仁见智了 大家可以参考截图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值