编辑框中加图片,以前一直以为很复杂,后来发现android有些类已经很好的实现了这些功能.

代码如下:

[java]  view plain copy
  1. mSubjectDetailView = (TextView) findViewById(R.id.subject_detail);  
  2.   
  3. CharSequence text = "如图所示★,dsfdsfdddd,如果fdsfs★东东";  
  4. SpannableStringBuilder builder = new SpannableStringBuilder(text);  
  5. String rexgString = "★";  
  6. Pattern pattern = Pattern.compile(rexgString);  
  7. Matcher matcher = pattern.matcher(text);  
  8.   
  9. while (matcher.find()) {  
  10.     builder.setSpan(  
  11.             new ImageSpan(this, R.drawable.ic_launcher), matcher.start(), matcher  
  12.                     .end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  13. }  
  14.   
  15. mSubjectDetailView.setText(builder);  
布局:

[html]  view plain copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/my_title"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_centerHorizontal="true"  
  12.         android:layout_centerVertical="true"  
  13.         android:text="@string/hello_world" />  
  14.      <TextView  
  15.                 android:id="@+id/subject_detail"  
  16.                 android:layout_width="match_parent"  
  17.                 android:layout_height="wrap_content"                  
  18.                 android:minHeight="50dp"  
  19.                 android:gravity="center_vertical"  
  20.                 android:text="subject"  
  21.                 android:textColor="@android:color/primary_text_light_nodisable"  
  22.                 android:background="@android:color/white"  
  23.                 android:textSize="25sp" />  
  24. RelativeLayout>  

效果:



android 图文结合,使用SpannableString和ImageSpan类

 

 Drawable drawable = getResources().getDrawable(id); 
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); 
        //需要处理的文本,[smile]是需要被替代的文本 
        SpannableString spannable = new SpannableString(getText().toString()+"[smile]"); 
        //要让图片替代指定的文字就要用ImageSpan 
        ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE); 
        //开始替换,注意第2和第3个参数表示从哪里开始替换到哪里替换结束(start和end) 
       //最后一个参数类似数学中的集合,[5,12)表示从5到12,包括5但不包括12 
        spannable.setSpan(span, getText().length(),getText().length()+"[smile]".length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);   
        setText(spannable); 


更多相关文章

  1. Android(安卓)屏幕常亮
  2. Android自动化测试MonkeyRunner
  3. Android(安卓)Activity界面切换添加动画特效
  4. Android之SharedPreferences对参数数据的存取
  5. android 获取Service(服务)的运行状态
  6. 【Appium + Python3】之安卓8.1,使用xpath定位不到元素
  7. Android(安卓)setTextColor 不生效
  8. Android二进制文件转码到Base64并通过Post进行提交
  9. Android第十二课 jni函数的静态绑定

随机推荐

  1. Android Studio解决plugin with id 'andr
  2. 动态绘制CheckedTextView
  3. Android UID 问题 uid 改变进行了覆盖安
  4. 介绍三个Android支持库控件:TabLayout+Vie
  5. Android百度地图开发(三)范围搜索
  6. Android横竖屏切换不重新调用onCreate()
  7. android中TextView跑马灯效果
  8. 如何减少RadioButton与文字的间距?
  9. Android如何查看应用签名信息--微信平台
  10. List of Android MIME types and Uri's