在安卓開發中我們需要利用TextView實現各種各樣的文字展示效果,這裏記錄常用的幾種利用TextView對文字的處理。效果圖:
一、利用TextView的ellipsize屬性實現跑馬燈輪播效果
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="中國民營企業500強發佈,華爲超聯想奪第一中國民營企業500強發佈,華爲超聯想奪第一" />
二、藉助自定義控件MarqueeView實現豎向輪播
<com.liuxingyu.textview.MarqueeView
android:id="@+id/marqueeview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/bg_marqueeview"
app:itemAnimationTime="700"
app:paddingLeft="10dp"
app:paddingTopBottom="15dp"
app:reRepeatDelayTime="1000"
app:startDelayTime="500"
app:textColor="#333333"
app:textSize="16sp" />
//豎向輪播
void VerticalMarqueeView() {
marqueeView.setTextArray(contentArray);
marqueeView.setOnItemClickListener(new MarqueeView.onItemClickListener() {
@Override
public void onItemClick(int position) {
Toast.makeText(MainActivity.this, contentArray[position], Toast.LENGTH_SHORT).show();
}
});
}
三、協議點擊效果(SpannableString)
這個是根據文字動態計算的,對各種各樣的都能夠處理的得心應手,缺點是利用TextView的SpannableString實現點擊事件就是文字本身,點擊範圍較小。
//協議點擊效果(SpannableString)
void SpannableString() {
SpannableString spanText = new SpannableString("《協議1》、《協議2》、《協議3》、《協議4》");
spanText.setSpan(new ClickableSpan() {
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(Color.RED); //設置文件顏色
ds.setUnderlineText(false); //設置下劃線
}
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "《協議1》", Toast.LENGTH_SHORT).show();
}
}, 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spanText.setSpan(new ClickableSpan() {
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(Color.RED); //設置文件顏色
ds.setUnderlineText(false); //設置下劃線
}
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "《協議2》", Toast.LENGTH_SHORT).show();
}
}, 6, 11, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv_agreement.setHighlightColor(Color.TRANSPARENT); //設置點擊後的顏色爲透明,否則會一直出現高亮
tv_agreement.setText(spanText);
tv_agreement.setMovementMethod(LinkMovementMethod.getInstance());//開始響應點擊事件
}
四、圖文混排效果(SpecialStringBuilder)
這裏使用的SpecialStringBuilder對文字進行處理,樣式相對來說會更多一點
```java
//圖文混排效果(SpecialStringBuilder)
void SpannableStringBuilder() {
SpecialStyle style = new SpecialStyle();
SpecialStringBuilder sb = new SpecialStringBuilder();
style.setColor(Color.RED);
style.setBackgroundColor(Color.WHITE);
style.setClickable(new ClickableStyle.OnClick() {
@Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this, "點擊了", Toast.LENGTH_SHORT).show();
}
});
style.clearUnderLine();
style.clearStrikethrough();
sb.append("白底紅字", style);
style.clearColor();
sb.append("紅底正常文字", style);
style.setColor(Color.WHITE);
style.setClickable(new ClickableStyle.OnClick() {
@Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this, "點擊了", Toast.LENGTH_SHORT).show();
}
});
style.setBackgroundColor(Color.BLUE);
sb.append("可以點擊事件的藍底文字", style);
style.clearStyles()
.setImage(new ImageSpan(this, R.mipmap.ic_launcher), false);
sb.append("變成圖片的文字", style);
style.setUnderLine(false);
sb.append("下劃線的文字", style);
style.setStrikethrough(false);
sb.append("刪除線的文字", style);
style.clearStyles();
sb.append("普通的文字", style);
tv_specialStringBuilder.setText(sb.getCharSequence());
tv_specialStringBuilder.setMovementMethod(LinkMovementMethod.getInstance());
}
五、圖文混排效果(HtmlString)
對於html基礎還記得的同學建議使用,樣式相對來說最全面。當然也可以考慮使用WebView控件。
//圖文混排效果(HtmlString)
void htmlString() {
tv_html.setText(Html.fromHtml(HTML_TEXT));
}