[功能]

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 运行截图:

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

更多相关文章

  1. Android(安卓)TV native层中Canvas库的实现思路
  2. Android设置透明状态栏,仿ios状态栏
  3. Android设计——Activity和Task的设计思路和方法
  4. Android(安卓)Studio 各种异常处理
  5. 另外两种android沉浸式状态栏实现思路
  6. 在eclipse环境中开发android原生应用
  7. Android(安卓)Studio精彩案例(三)《模仿微信ViewPage+Fragment实
  8. 基于RecyclerView实现模仿IOS风格的滚轮选择器(一)
  9. ios模仿android屏幕密度控件自动适配

随机推荐

  1. Android(安卓)列表中设置Button后setOnIt
  2. Android中摇一摇效果的实现
  3. android 导航总结
  4. Flutter(Android(安卓)混合开发)
  5. Flutter中高级培训
  6. java.net.SocketTimeoutException: Conne
  7. android 多点触摸实例
  8. Android开发实战一之搭建开发环境(最新版
  9. Android 代码设置RelativeLayout元素居中
  10. Android(安卓)篡改apk文件的可行性分析