Android開(kāi)發(fā)實(shí)現(xiàn)在TextView前面加標(biāo)簽示例
效果

代碼實(shí)現(xiàn): 自定義TextView
public class TagTextView extends AppCompatTextView {
private View view;//標(biāo)簽布局的最外層布局
private Context mContext;
private TextView tv_tag;
private StringBuffer content_buffer;
//必須重寫(xiě)所有的構(gòu)造器,否則可能會(huì)出現(xiàn)無(wú)法inflate布局的錯(cuò)誤!
public TagTextView(Context context) {
super(context);
mContext = context;
}
public TagTextView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
}
public TagTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
}
public void setContentAndTag(String content, List<String> tags) {
content_buffer = new StringBuffer();
for (String item : tags) {//將每個(gè)tag的內(nèi)容添加到content后邊,之后將用drawable替代這些tag所占的位置
content_buffer.append(item);
}
content_buffer.append(content);
SpannableString spannableString = new SpannableString(content_buffer);
for (int i = 0; i < tags.size(); i++) {
String item = tags.get(i);
View view = LayoutInflater.from(mContext).inflate(R.layout.layout_texttab, null);//R.layout.tag是每個(gè)標(biāo)簽的布局
tv_tag = view.findViewById(R.id.tv_tag);
tv_tag.setText(item);
Bitmap bitmap = convertViewToBitmap(view);
Drawable d = new BitmapDrawable(bitmap);
d.setBounds(0, 0, tv_tag.getWidth(), tv_tag.getHeight());//缺少這句的話,不會(huì)報(bào)錯(cuò),但是圖片不回顯示
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BOTTOM);//圖片將對(duì)齊底部邊線
int startIndex;
int endIndex;
startIndex = getLastLength(tags, i );
endIndex = startIndex + item.length();
Log.e("tag", "the start is" + startIndex + "the end is" + endIndex);
spannableString.setSpan(span, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
setText(spannableString);
setGravity(Gravity.CENTER_VERTICAL);
}
private static Bitmap convertViewToBitmap(View view) {
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
return bitmap;
}
private int getLastLength(List<String> list, int maxLength) {
int length = 0;
for (int i = 0; i < maxLength; i++) {
length += list.get(i).length();
}
return length;
}
}
自定義一個(gè)layout_texttab布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_tag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginBottom="2dp"
android:layout_marginRight="5dp"
android:background="@drawable/round_blue"
android:gravity="center_vertical"
android:paddingBottom="2dp"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:text="標(biāo)簽"
android:textColor="@android:color/white"
android:textSize="12sp"/>
</LinearLayout>
使用
<com.zc.tool.TagTextView
android:id="@+id/text_tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:textColor="@color/white"
android:gravity="center" />
List<String> tags = new ArrayList<>();
tags.add("@用戶(hù)");
tags.add("@程序猿");
tags.add("@產(chǎn)品經(jīng)理");
textTab.setContentAndTag("改個(gè)需求", tags);
以上就是Android開(kāi)發(fā)實(shí)現(xiàn)在TextView前面加標(biāo)簽示例的詳細(xì)內(nèi)容,更多關(guān)于Android開(kāi)發(fā)TextView前加標(biāo)簽的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
android仿愛(ài)奇藝加載動(dòng)畫(huà)實(shí)例
這篇文章主要介紹了android仿愛(ài)奇藝加載動(dòng)畫(huà)實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。2016-10-10
解決Android Studio 格式化 Format代碼快捷鍵問(wèn)題
這篇文章主要介紹了解決Android Studio 格式化 Format代碼快捷鍵問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Android學(xué)習(xí)之Flux架構(gòu)入門(mén)
Flux是Facebook在14年提出的一種Web前端架構(gòu),主要用來(lái)處理復(fù)雜的UI邏輯的一致性問(wèn)題(當(dāng)時(shí)是為了解決Web頁(yè)面的消息通知問(wèn)題)。接下來(lái)從其特點(diǎn)和使用上來(lái)介紹Flux架構(gòu)。本文主要目的是讓你對(duì)Flux的一個(gè)架構(gòu)大體面貌有個(gè)了解。2016-08-08
Android獲取觸摸手勢(shì)實(shí)現(xiàn)左右滑動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android獲取觸摸手勢(shì)實(shí)現(xiàn)左右滑動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
android 應(yīng)用內(nèi)部懸浮可拖動(dòng)按鈕簡(jiǎn)單實(shí)現(xiàn)代碼
本篇文章主要介紹了android 應(yīng)用內(nèi)部懸浮可拖動(dòng)按鈕簡(jiǎn)單實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
Android ROM升級(jí)包的選項(xiàng)解釋
本文主要解釋 Android刷機(jī)過(guò)程中會(huì)遇到apply sdcard:update.zip(刷ROM升級(jí)包)的選項(xiàng),對(duì)選項(xiàng)解釋?zhuān)悦庠斐蓻](méi)必要的麻煩,希望對(duì)大家有用!2016-07-07
android:照片涂畫(huà)功能實(shí)現(xiàn)過(guò)程及原理詳解
這篇文章主要介紹了android:照片涂畫(huà)功能實(shí)現(xiàn)過(guò)程及原理,需要的朋友可以參考下2014-02-02
Android自定義View實(shí)現(xiàn)葉子飄動(dòng)旋轉(zhuǎn)效果(四)
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)葉子飄動(dòng)旋轉(zhuǎn)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03

