游戏详情自定义栏目富文本正文兼容低版本安卓系统

This commit is contained in:
juntao
2020-06-23 16:44:28 +08:00
parent a24d0a9618
commit 8f4c6abfd3
3 changed files with 32 additions and 10 deletions

View File

@ -10,6 +10,7 @@ import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.View;
@ -66,7 +67,6 @@ public class ExpandTextView extends AppCompatTextView {
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// TODO 复用有问题
if (mShowExpandTextRegardlessOfMaxLines && !mIsExpanded) {
updateMaxLines();
}
@ -75,7 +75,8 @@ public class ExpandTextView extends AppCompatTextView {
private void updateMaxLines() {
mMaxLines = getLineCount() - 1;
mMaxLinesCalculatedCallback.onMaxLinesCalculated(getLineCount() - 1);
setMaxLines(mMaxLines);
mMaxLinesCalculatedCallback.onMaxLinesCalculated(mMaxLines);
}
@Override
@ -211,7 +212,18 @@ public class ExpandTextView extends AppCompatTextView {
}
}
}, startPosition + mEndText.length(), msp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
msp.setSpan(new GradientAlphaTextSpan(), startPosition, startPosition + finalEndText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
int paintColor = paint.getColor();
// 找到最后一个 ForegroundSpan 并获取颜色给 GradientAlphaTextSpan
ForegroundColorSpan[] foregroundColorSpans = msp.getSpans(0, startPosition, ForegroundColorSpan.class);
if (foregroundColorSpans.length != 0) {
int endPosition = msp.getSpanEnd(foregroundColorSpans[foregroundColorSpans.length - 1]);
if (endPosition == startPosition) {
paintColor = foregroundColorSpans[foregroundColorSpans.length - 1].getForegroundColor();
}
}
msp.setSpan(new GradientAlphaTextSpan(paintColor), startPosition, startPosition + finalEndText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
setText(msp);
setMovementMethod(CustomLinkMovementMethod.getInstance());