在textView中添加 drawable

  1. 使用ImageSpan:
//SpannableString implements CharSequence
Drawable drawable=mContext.getResources().getDrawable(R.mipmap.bangtang);
//必须为 Drawable 设置边界,才可以显示出来
drawable.setBounds(0,0,drawable.getIntrinsicWidth()/2,drawable.getIntrinsicHeight()/2);
ImageSpan imageSpan=new ImageSpan(drawable,ImageSpan.ALIGN_BOTTOM);
SpannableString spanString=new SpannableString(""+"textView 的 text");
spanString.setSpan(imageSpan,0,1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spanString);

    /**
     * 
     * @param what  各种Span的类型
     * @param start 要替代的文本内容的start
     * @param end   要替代的文本内容的end,比如要放在开头,则 start=0,end=1
     * @param flags 用来标识在Span范围内的文本前后输入新的字符时是否把它们也应用这个效果
     */
    public void setSpan(Object what, int start, int end, int flags)
    /** flags类型
     * Non-0-length spans of type SPAN_INCLUSIVE_EXCLUSIVE expand
     * to include text inserted at their starting point but not at their
     * ending point.  When 0-length, they behave like marks.
     */
    public static final int SPAN_INCLUSIVE_EXCLUSIVE = SPAN_MARK_MARK;
    /**
     * Spans of type SPAN_INCLUSIVE_INCLUSIVE expand
     * to include text inserted at either their starting or ending point.
     */
    public static final int SPAN_INCLUSIVE_INCLUSIVE = SPAN_MARK_POINT;
    /**
     * Spans of type SPAN_EXCLUSIVE_EXCLUSIVE do not expand
     * to include text inserted at either their starting or ending point.
     * They can never have a length of 0 and are automatically removed
     * from the buffer if all the text they cover is removed.
     */
    public static final int SPAN_EXCLUSIVE_EXCLUSIVE = SPAN_POINT_MARK;
    /**
     * Non-0-length spans of type SPAN_EXCLUSIVE_INCLUSIVE expand
     * to include text inserted at their ending point but not at their
     * starting point.  When 0-length, they behave like points.
     */
    public static final int SPAN_EXCLUSIVE_INCLUSIVE = SPAN_POINT_POINT;
  1. 用TextView本身的属性同时显示图片和文字
    <TextView
        android:drawableLeft=""
        android:drawableStart=""
        android:drawableRight=""
        android:drawableEnd=""
        android:drawableTop=""
        android:drawableBottom=""
        android:drawablePadding=""/>

TextView 有这些属性: drawableLeft是指定drawable放在文本的左边,其他类推; drawableStart与drawableLeft类似; drawablePadding指定文本和drawable之间padding

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容