網站首頁 編程語言 正文
效果
代碼實現: 自定義TextView
public class TagTextView extends AppCompatTextView {
private View view;//標簽布局的最外層布局
private Context mContext;
private TextView tv_tag;
private StringBuffer content_buffer;
//必須重寫所有的構造器,否則可能會出現無法inflate布局的錯誤!
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) {//將每個tag的內容添加到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是每個標簽的布局
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());//缺少這句的話,不會報錯,但是圖片不回顯示
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BOTTOM);//圖片將對齊底部邊線
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;
}
}
自定義一個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="標簽" 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("@用戶");
tags.add("@程序猿");
tags.add("@產品經理");
textTab.setContentAndTag("改個需求", tags);
原文鏈接:https://blog.csdn.net/qq_40945489/article/details/109399596
相關推薦
- 2022-09-03 Pycharm中運行程序在Python?console中執行,不是直接Run問題_python
- 2022-05-25 Go語言的type?func()用法詳解_Golang
- 2022-07-21 常見CSS樣式
- 2022-06-30 PyTorch詳解經典網絡ResNet實現流程_python
- 2022-11-14 Git暫存區的意義或git add的意義
- 2022-04-12 一篇文章帶你了解python中的typing模塊和類型注解_python
- 2022-06-07 教你使用Jenkins集成Harbor自動發布鏡像_docker
- 2022-10-13 Python線性表種的單鏈表詳解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支