Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to reduce TextView line spacing
    text
    copied!<p>I am trying to reduce the line spacing in a TextView by setting a negative 'add' to TextView.setLineSpacing(). It works well except that the bottom line get truncated. </p> <p>Main layout</p> <p></p> <pre><code>&lt;TextView android:id="@+id/text_view" android:padding="dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" tools:context=".MainActivity" /&gt; </code></pre> <p></p> <p>Main activity: (notice the </p> <pre><code>package com.font_test; import android.app.Activity; import android.graphics.Typeface; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/custom_fonts.ttf"); final TextView tv = (TextView) findViewById(R.id.text_view); tv.setTypeface(typeface); tv.setTextSize(60); tv.setLineSpacing(-30f, 1f); // *** -30 to reduce line spacing tv.setBackgroundColor(0x280000ff); tv.setText("gggkiiikkk" + "\n" + "gikgikgik" + "\n" + "kigkigkig"); } } </code></pre> <p>This results in truncation at the bottom of the view (notice the 'g' at the bottom line):</p> <p><img src="https://i.stack.imgur.com/2GOOT.png" alt="Reducing the line spacing cuts the &#39;g&#39; at the bottom line"></p> <p>It seems that the problem is related to incorrect layout measurement. If I set the TextView to</p> <pre><code> android:layout_height="fill_parent" </code></pre> <p>It does render properly:</p> <p><img src="https://i.stack.imgur.com/zyEOW.png" alt="Using &#39;fill_parent&#39; does not truncate the bottom line"></p> <p>Any idea how to fix it? I don't mind to have ugly workarounds if it helps. I also have access to FontForge and I can modify the font file if needed.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload