ClickSpan 点击事件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/color_bcb"/>
<item android:color="@color/transparent"/>
</selector>
-------------------------------------------------------------------------
<TextView
android:id="@+id/tv_like"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColorLink="@color/hyperlink_selector"
android:textColorHighlight="@color/color_bcb"
android:textColor="@color/color_57"
android:textSize="@dimen/text_size_14dp"/>
-------------------------------------------------------------------------
SpannableStringBuilder likes = new SpannableStringBuilder();
span.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(getResources().getColor(R.color.color_57));
ds.setUnderlineText(false);
}
}, 0, span.length() - 2, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
likes.append(span);
tvLike.setText(likes);
tvLike.setMovementMethod(LinkMovementMethod.getInstance());
-------------------------------------------------------------------------
package abc;
import android.content.Context;
import android.text.Layout;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.style.ClickableSpan;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.TextView;
/**
* Created by AWei on 2015/4/13.
*
*/
public class SpannableTextView extends TextView {
public SpannableTextView(Context context) {
super(context);
}
public SpannableTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SpannableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean result = super.onTouchEvent(event);
int x = (int) event.getX();
int y = (int) event.getY();
x -= getTotalPaddingLeft();
y -= getTotalPaddingTop();
x += getScrollX();
y += getScrollY();
Layout layout = getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
CharSequence text = getText();
if (TextUtils.isEmpty(text) || !(text instanceof Spannable)) {
return result;
}
Spannable buffer = (Spannable) text;
ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
if (link.length > 0) {
return true;
} else {
return false;
}
}
}
TypefaceSpan 字体
msp.setSpan(new TypefaceSpan("monospace"), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
AbsoluteSizeSpan 大小
msp.setSpan(new AbsoluteSizeSpan(20), 4, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
msp.setSpan(new RelativeSizeSpan(0.5f), 8, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ForegroundColorSpan 颜色
msp.setSpan(new ForegroundColorSpan(Color.MAGENTA), 12, 15,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
BackgroundColorSpan 背景色
msp.setSpan(new BackgroundColorSpan(Color.CYAN), 15, 18, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
StyleSpan 样式
msp.setSpan(new StyleSpan(android.graphics.Typeface.NORMAL), 18, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //正常
msp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 20, 22, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //粗体
msp.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 22, 24, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //斜体
msp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 24, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //粗斜体