Android富文本图文混排、粗体、斜体、图片、下划线、删除线等

       /**
	 * 粗体
	 */
	private void addBoldSpan(String str) {
		SpannableString spanString = new SpannableString(str);
		StyleSpan span = new StyleSpan(Typeface.BOLD);
		spanString.setSpan(span, 0, str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		content_txt.append(spanString);
	}
	
	/**
	 * 斜体
	 */
	private void addItalicSpan(String str) {
		SpannableString spanString = new SpannableString(str);
		StyleSpan span = new StyleSpan(Typeface.ITALIC);
		spanString.setSpan(span, 0, str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		content_txt.append(spanString);
	}

	/**
	 * 删除线
	 */
	private void addStrikeSpan(String str) {
		SpannableString spanString = new SpannableString(str);
		StrikethroughSpan span = new StrikethroughSpan();
		spanString.setSpan(span, 0, str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		content_txt.append(spanString);
	}

	/**
	 * 下划线
	 */
	private void addUnderLineSpan(String str) {
		SpannableString spanString = new SpannableString("下划线");
		UnderlineSpan span = new UnderlineSpan();
		spanString.setSpan(span, 0, str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		content_txt.append(spanString);
	}

	/**
	 * 图片
	 */
	private void addImageSpan(Bitmap bitmap) {
		SpannableString spanString = new SpannableString(" ");
		@SuppressWarnings("deprecation")
		Drawable d = new BitmapDrawable(bitmap);
		d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
		ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
		spanString.setSpan(span, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		content_txt.append(spanString);
	}

你可能感兴趣的:(Android)