Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to tag TextView in front of Android developer

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report