android 高亮字符,TextView 部分字符高亮 android

[功能]

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

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

TextView item = (TextView)lLayout.getChildAt(i);

item.setTextColor(c);

}

}

}

public void addBackResource(int r){

lLayout.setBackgroundResource(r);

}

public View loadView(){

return lLayout;

}

}

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));

}

}

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("红色其它颜色"));

ll.addView(tv);

}

}

txt_high_light.xml

android:orientation="vertical"

android:id="@+id/layout"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:text="HelloText1"

android:layout_width="fill_parent"

android:layout_height="wrap_content" />

7. emulator 运行截图:

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值