TextView 部分字符高亮 android

来源:互联网 发布:淘宝飞利浦吹风机 编辑:程序博客网 时间:2024/05/20 10:13

[功能]

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

 

 

[思路]

1. 利用LinearLayout 作为 TextView 的 容器

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

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

 

 

[思路 步骤]

 

TextHighlightHelper.java

public class TextHighlightHelper {

    Activity activity; 
         
    LinearLayout lLayout; 
         
    public TextHighlightHelper(Activity a,int l){ 
      activity = a; 
       
      lLayout = new LinearLayout(activity); 
      lLayout.setOrientation(l); 
      lLayout.setLayoutParams( 
        new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT)); 
    }
   
    public void addText(CharSequence cs){ 
        for(int i=0;i<cs.length();i++){ 
         TextView tv = new TextView(activity); 
         tv.setText(cs.charAt(i)+""); 
          
         lLayout.addView(tv); 
        } 
       }

      public void addColor(int s,int l,int c){ 
        if(l > lLayout.getChildCount()){ 
         //error argument 
        } 
        else { 
         for(int i=s;i<s+l;i++){ 
          TextView item = (TextView)lLayout.getChildAt(i); 
           
          item.setTextColor(c); 
         } 
        } 
       } 

      public void addBackResource(int r){ 
        lLayout.setBackgroundResource(r); 
       }

      public View loadView(){ 
            return lLayout; 
      } 
}

 

 

TextHighlightUsage.java

public class TextHighlightUsage extends Activity{

    TextHighlightHelper tHelper1; 
    TextHighlightHelper tHelper2; 
     
    LinearLayout ll; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.txt_high_light); 
         
        ll = (LinearLayout)findViewById(R.id.layout); 
         
        //Text:HelloText2 
        CharSequence c1 = "HelloText2"; 
        tHelper1 = new TextHighlightHelper(this,LinearLayout.HORIZONTAL); 
        tHelper1.addText(c1); 
        tHelper1.addColor(0, 3, Color.RED); 
        tHelper1.addBackResource(R.drawable.icon); 
         
        ll.addView(tHelper1.loadView()); 
         
        //Text:创新源于模仿! 
        CharSequence c2 = "创新源于模仿!"; 
        tHelper2 = new TextHighlightHelper(this,LinearLayout.VERTICAL); 
        tHelper2.addText(c2); 
        tHelper2.addColor(1, 3, Color.RED); 
         
        ll.addView(tHelper2.loadView()); 

 

        TextView tv = new TextView(this);
        tv.setText(Html.fromHtml("<font color=/"#ff0000/">红色</font>其它颜色"));
        ll.addView(tv);

 

    }
}

txt_high_light.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:id="@+id/layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<TextView  
    android:text="HelloText1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 
</LinearLayout>

 

7. emulator 运行截图:

 txt_high_light.png

From:http://griffinshi.javaeye.com/blog/604444

原创粉丝点击