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 set indent distance dynamically by TextView in Android

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this article, the editor introduces in detail "how to dynamically set indent distance in TextView in Android". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to dynamically set indent distance in TextView in Android" can help you solve your doubts.

The requirement is to add a label display to the front end of the TextView.

The final effect is as follows:

According to the effect diagram, it is easy to think of using SpannableStringBuilder, and it is the LeadingMarginSpan class that is used here.

Official note: A paragraph style affecting the leading margin. There can be multiple leading margin spans on a single paragraph; they will be rendered in order, each adding its margin to the ones before it. The leading margin is on the right for lines in a right-to-left paragraph.

LeadingMarginSpans should be attached from the first character to the last character of a single paragraph.

To put it simply, set the indent distance of the paragraph.

Related API usage:

Val spannableString = SpannableString (text) val what = LeadingMarginSpan.Standard (width, 0) spannableString.setSpan (what, 0, spannableString.length, SpannableString.SPAN_INCLUSIVE_INCLUSIVE)

LeadingMarginSpan is the interface, and the internal Standard is its standard implementation. There are two constructors, Standard (int every) and Standard (int first, int rest). Standard (int every) sets the same indent distance for each row, while Standard (int first, int rest) sets the indent distance for the first line and other lines respectively. What we use here is the Standard (int first, int rest) implementation. Next, the setSpan method, if you have used other ForegroundColorSpan, AbsoluteSizeSpan and other span is not unfamiliar. Pass in four parameters, the first parameter is the created Span, the second parameter and the third parameter are the scope of Span, the fourth parameter indicates whether to include the front and rear boundaries, the INCLUSIVE indicates the inclusion boundary, and the EXCLUSIVE does not include the boundary.

Once you know how to set the indent distance, the next step is to get the width of the label. The label is View, and the exact width can only be obtained after the View rendering is complete. The method used here is to get the viewTreeObserver of the tag View, and then addOnPreDrawListener. You need to pay attention to removeOnPreDrawListener as soon as the width is obtained. The specific implementation is as follows:

Class MainActivity2: AppCompatActivity () {override fun onCreate (savedInstanceState: Bundle?) {super.onCreate (savedInstanceState) setContentView (R.layout.activity_main) val tv1 = findViewById (R.id.tv1) val tv2 = findViewById (R.id.tv2) tv1.text = "New" calculateTag (tv1, tv2, "This is a long long long long title")} / / dynamically set indent distance fun calculateTag (tag: TextView) Title: TextView, text: String?) {val observer = tag.viewTreeObserver observer.addOnPreDrawListener (object: ViewTreeObserver.OnPreDrawListener {override fun onPreDraw (): Boolean {val spannableString = SpannableString (text) val what = LeadingMarginSpan.Standard (tag.width + dip2px (this@MainActivity2, 3.0), 0) spannableString.setSpan (what, 0) SpannableString.length, SpannableString.SPAN_INCLUSIVE_INCLUSIVE) title.text = spannableString tag.viewTreeObserver.removeOnPreDrawListener (this) return false}})} fun dip2px (context: Context DpValue: Double): Int {val density: Float = context.resources.displayMetrics.density return (dpValue * density + 0.5) .toInt ()}}

Layout file:

After reading this, the article "how to dynamically set indent distance in TextView in Android" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

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