FragmentPagerAdapter title设置文本属性 比如最大行数 设置超过maxems=7时换行
时间: 2023-06-02 09:02:56 浏览: 131
您可以在FragmentPagerAdapter的getPageTitle()方法中返回SpannableString对象,并在其中设置您想要的文本属性,例如最大行数和换行。以下是一个示例代码:
```
@Override
public CharSequence getPageTitle(int position) {
String title = "Your Title Here";
SpannableString spannableString = new SpannableString(title);
int maxLines = 2;
int maxEms = 7;
TextUtils.TruncateAt truncateAt = TextUtils.TruncateAt.END;
spannableString = new SpannableString(title);
spannableString.setSpan(new TextAppearanceSpan(context, R.style.TitleTextStyle), 0, title.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new MaxLinesSpan(maxLines), 0, title.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new MaxEmsSpan(maxEms), 0, title.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new EllipsesSpan(truncateAt), 0, title.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return spannableString;
}
```
其中,TitleTextStyle是您定义的文本样式,MaxLinesSpan、MaxEmsSpan和EllipsesSpan是自定义的Span类,用于设置最大行数、最大字符数和省略号。以下是这些Span类的示例实现:
```
public class MaxLinesSpan extends MetricAffectingSpan {
private int maxLines;
public MaxLinesSpan(int maxLines) {
this.maxLines = maxLines;
}
@Override
public void updateDrawState(TextPaint tp) {
// Do nothing
}
@Override
public void updateMeasureState(TextPaint p) {
p.setTextSize(p.getTextSize());
p.setFakeBoldText(p.isFakeBoldText());
p.setUnderlineText(p.isUnderlineText());
p.setColor(p.getColor());
p.setTypeface(p.getTypeface());
p.setTextScaleX(p.getTextScaleX());
p.setTextSkewX(p.getTextSkewX());
p.setStrikeThruText(p.isStrikeThruText());
p.setElegantTextHeight(p.isElegantTextHeight());
p.setSubpixelText(p.isSubpixelText());
p.setHinting(p.getHinting());
p.setMetrics(p.getFontMetricsInt());
p.setShadowLayer(p.getShadowLayerRadius(), p.getShadowLayerDx(), p.getShadowLayerDy(), p.getShadowLayerColor());
p.setLineSpacing(p.getLineSpacingExtra(), p.getLineSpacingMultiplier());
p.setLetterSpacing(p.getLetterSpacing());
p.setMaxLines(maxLines);
}
}
public class MaxEmsSpan extends MetricAffectingSpan {
private int maxEms;
public MaxEmsSpan(int maxEms) {
this.maxEms = maxEms;
}
@Override
public void updateDrawState(TextPaint tp) {
// Do nothing
}
@Override
public void updateMeasureState(TextPaint p) {
p.setTextSize(p.getTextSize());
p.setFakeBoldText(p.isFakeBoldText());
p.setUnderlineText(p.isUnderlineText());
p.setColor(p.getColor());
p.setTypeface(p.getTypeface());
p.setTextScaleX(p.getTextScaleX());
p.setTextSkewX(p.getTextSkewX());
p.setStrikeThruText(p.isStrikeThruText());
p.setElegantTextHeight(p.isElegantTextHeight());
p.setSubpixelText(p.isSubpixelText());
p.setHinting(p.getHinting());
p.setMetrics(p.getFontMetricsInt());
p.setShadowLayer(p.getShadowLayerRadius(), p.getShadowLayerDx(), p.getShadowLayerDy(), p.getShadowLayerColor());
p.setEms(maxEms);
}
}
public class EllipsesSpan extends ReplacementSpan {
private TextUtils.TruncateAt truncateAt;
public EllipsesSpan(TextUtils.TruncateAt truncateAt) {
this.truncateAt = truncateAt;
}
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
return (int) paint.measureText(text, start, end);
}
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
canvas.drawText(TextUtils.ellipsize(text, paint, x, truncateAt), x, y, paint);
}
}
```
阅读全文