Get the App
SLTechnology News&Howtos  ›  Development  › 

How to tag TextView in front of Android developer

Shulou Source: shulou.com Published: 2022-05-31 19:09:58 09月13日 Update

This article mainly introduces "Android development how to tag in front of TextView", in daily operation, I believe many people in Android development how to achieve tagging in front of TextView have doubts, editor looked up all kinds of information, sorted out a simple and easy-to-use method of operation, hope to answer the "Android development how to achieve tagging in front of TextView" doubt helpful! Next, please follow the editor to study!

Effect.

Code implementation: customize the outermost layout of the TextViewpublic class TagTextView extends AppCompatTextView {private View view;// tag layout private Context mContext; private TextView tv_tag; private StringBuffer content_buffer; / / all constructors must be rewritten, otherwise there may be an error that cannot inflate the layout! 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 tags) {content_buffer = new StringBuffer () For (String item: tags) {/ / add the contents of each tag to the end of the content, and then replace the position occupied by these tag content_buffer.append (item) with drawable;} content_buffer.append (content); SpannableString spannableString = new SpannableString (content_buffer); for (int I = 0; I < tags.size (); iTunes +) {String item = tags.get (I) View view = LayoutInflater.from (mContext) .propagate (R.layout.layout_texttab, null); / / R.layout.tag is the layout of each 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 ()); / / if this sentence is missing, no error will be reported, but the picture will not show ImageSpan span = new ImageSpan (d, ImageSpan.ALIGN_BOTTOM); / / the picture will align the bottom border 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 list, int maxLength) {int length = 0; for (int I = 0; I < maxLength; iTunes +) {length + = list.get (I). Length ();} return length;}} customize a layout_texttab layout using List tags = new ArrayList (); tags.add ("@ user") Tags.add ("@ programmer"); tags.add ("@ product manager"); textTab.setContentAndTag ("change requirements", tags); at this point, the study on "how to tag Android development in front of TextView" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Tags layout development learning pictures more help practical next product code location content outer layer bottom effects articles methods theories users Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MySQL Shulou Information macOS Huawei