Individual line spacing for each line

Can I set separate line spacing for each TextView text string?

Example:

 TextView tv = new TextView(context); tv.setText("line1\nline2\nline3"); 

The setLineSpacing(float add, float mult) determines line spacing for all TextView text strings. I would like to define another line spacing between lines 1 and 2 and another line spacing between lines 2 and 3.

Any ideas how to do this?

Does spannable provide a solution?

+8
android textview
Jun 20 2018-12-12T00: 00Z
source share
4 answers

Yes, you can do this using the LineHeightSpan interface. Here is a quick and dirty code example on how to do this:

 public class MyActivity extends Activity { private static class MySpan implements LineHeightSpan { private final int height; MySpan(int height) { this.height = height; } @Override public void chooseHeight(CharSequence text, int start, int end, int spanstartv, int v, FontMetricsInt fm) { fm.bottom += height; fm.descent += height; } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final TextView tv = new TextView(this); setContentView(tv); tv.setText("Lines:\n", BufferType.EDITABLE); appendLine(tv.getEditableText(), "Line 1 = 40\n", 40); appendLine(tv.getEditableText(), "Line 2 = 30\n", 30); appendLine(tv.getEditableText(), "Line 3 = 20\n", 20); appendLine(tv.getEditableText(), "Line 4 = 10\n", 10); } private void appendLine(Editable text, String string, int height) { final int start = text.length(); text.append(string); final int end = text.length(); text.setSpan(new MySpan(height), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } 
+16
Jun 20 2018-12-12T00:
source share

you can use these specific styles for text viewing using html. Try this example

 tv.setText(Html.fromHtml("<h2>Text1</h2><br><p>Text2</p>")); 

Different types of tags are possible here.

 <a href="..."> <b> <big> <blockquote> <br> <cite> <dfn> <div align="..."> <em> <font size="..." color="..." face="..."> <h1> <h2> <h3> <h4> <h5> <h6> <i> <img src="..."> <p> <small> <strike> <strong> <sub> <sup> <tt> <u> 
+1
Jun 20 2018-12-12T00:
source share

Impossible. This technique is used in c, C ++. What you use inside setText () shows all the text you write.

-3
Jun 20 2018-12-12T00:
source share

Try the following:

 android:lineSpacingMultiplier = "3" 

The spacing between lines can also be in decimal format.

-3
Mar 31 '15 at 9:15
source share



All Articles