zoukankan      html  css  js  c++  java
  • TextView和EditText中添加图片(ImageSpan)

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

    代码如下:

    [java] view plaincopy
    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 plaincopy
    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>  

    效果:



  • 相关阅读:
    职场“潜”规则:心法和技法
    JVM参数配置
    python-编码
    pyserial库-串口通讯模块
    Arduino-舵机
    Arduino-中断
    Arduino-一些函数
    Arduino-位操作
    Nginx (一)Windows下编译Nginx源码以及安装 nginx for windows方法步骤
    MSYS2环境搭建
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6469792.html
Copyright © 2011-2022 走看看