游戏详情自定义栏目支持富文本
This commit is contained in:
@ -25,11 +25,10 @@ public class ExpandTextView extends AppCompatTextView {
|
||||
private String mEndText = "...";
|
||||
private String mExpandText = mEndText + "全文";
|
||||
private boolean mUseGradientAlphaEndText = false;
|
||||
private boolean mShrinkOnAnchor = false;
|
||||
|
||||
private int mMaxLines = 3; // 由于sdk版本限制(getMaxLines) 这里设置默认值
|
||||
|
||||
private static int DEFAULT_ADDITIONAL_END_TEXT_COUNT = 2;
|
||||
|
||||
private boolean mInitLayout = false;
|
||||
private boolean mIsExpanded = false; // 位于 recyclerView 时需要自行在外层管理是否已展开
|
||||
|
||||
@ -38,6 +37,9 @@ public class ExpandTextView extends AppCompatTextView {
|
||||
private Rect mLastVisibleLineRect;
|
||||
private Rect mLastActualLineRect;
|
||||
|
||||
private static int DEFAULT_ADDITIONAL_END_TEXT_COUNT = 2;
|
||||
public static final String SHRINK_ANCHOR = "☼";
|
||||
|
||||
public ExpandTextView(Context context) {
|
||||
super(context);
|
||||
}
|
||||
@ -52,6 +54,7 @@ public class ExpandTextView extends AppCompatTextView {
|
||||
mLastActualLineRect = new Rect();
|
||||
|
||||
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ExpandTextView);
|
||||
mShrinkOnAnchor = ta.getBoolean(R.styleable.ExpandTextView_shrinkOnAnchor, false);
|
||||
mUseGradientAlphaEndText = ta.getBoolean(R.styleable.ExpandTextView_useGradientAlphaEndText, false);
|
||||
mEndText = ta.getString(R.styleable.ExpandTextView_endText) == null ? mEndText : ta.getString(R.styleable.ExpandTextView_endText);
|
||||
mExpandText = ta.getString(R.styleable.ExpandTextView_expandText) == null ? mExpandText : ta
|
||||
@ -62,9 +65,31 @@ public class ExpandTextView extends AppCompatTextView {
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
||||
if (mShrinkOnAnchor) {
|
||||
updateMaxLinesByShrinkAnchor();
|
||||
}
|
||||
setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight() - getExtraBottomPadding());
|
||||
}
|
||||
|
||||
private void updateMaxLinesByShrinkAnchor() {
|
||||
int count = getLineCount();
|
||||
for (int line = 0; line < count; line++) {
|
||||
int start = getLayout().getLineStart(line);
|
||||
int end = getLayout().getLineEnd(line);
|
||||
CharSequence substring = getText().subSequence(start, end);
|
||||
if (substring.toString().contains(SHRINK_ANCHOR)) {
|
||||
if (mMaxLines != line + 1) {
|
||||
setExpandMaxLines(line + 1);
|
||||
int anchorIndex = getText().toString().indexOf(SHRINK_ANCHOR);
|
||||
SpannableStringBuilder sb = new SpannableStringBuilder(getText());
|
||||
sb.delete(anchorIndex, anchorIndex + 1);
|
||||
setText(sb);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
|
||||
super.onLayout(changed, left, top, right, bottom);
|
||||
|
||||
Reference in New Issue
Block a user