好好的textView前面加了個(gè)標(biāo)簽...
這要是單行還好梯捕,直接一個(gè)drawable了事
但是多行...我可以自定義一個(gè)ShapeDrawable,實(shí)現(xiàn)drawableLeft,但是有個(gè)問題,如果是這樣的話退疫,drawable是會(huì)隨字體居中的参歹,那顯示效果就很怪異了!
一岔霸、通過span
這個(gè)是最靠譜的解決方法了吧薛躬,其實(shí)span可以幫我們解決幾乎所有關(guān)于textview的問題,可是貌似很多人(包括我)都不了解她呆细。
具體可以了解下
https://blog.csdn.net/qq_16430735/article/details/50427978
二型宝、自定義TextView
廢話少說,直接上工具類代碼:
public class TagTextView extends AppCompatTextView {
private StringBuffer content_buffer;
private TextView tv_tag;
private View view;//標(biāo)簽布局的最外層布局
private Context mContext;
//必須重寫所有的構(gòu)造器,否則可能會(huì)出現(xiàn)無法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.tag, 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;
}
}
tag布局:
<?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_marginEnd="5dp"
android:background="@drawable/bg_blue_20"
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>
layout_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.TagTextViewActivity">
<com.smt.saveviewtocream.utils.TagTextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
具體使用(kotlin)
class TagTextViewActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_tag_text_view)
val tags: MutableList<String> = ArrayList()
tags.add("置頂")
tags.add("置頂1")
tags.add("置頂2")
textView.setContentAndTag(
"今天是個(gè)好日子",
tags
)
}
}
結(jié)果: