android – 如何减少TextView行间距

前端之家收集整理的这篇文章主要介绍了android – 如何减少TextView行间距前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图通过设置TextView.setLineSpacing()的负“添加”来减少TextView中的行间距.除了底线被截断之外,它运行良好.

主要布局

  1. <TextView
  2. android:id="@+id/text_view"
  3. android:padding="dp"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. android:layout_centerHorizontal="true"
  7. android:layout_centerVertical="true"
  8. tools:context=".MainActivity" />

主要活动:(注意

  1. package com.font_test;
  2.  
  3. import android.app.Activity;
  4. import android.graphics.Typeface;
  5. import android.os.Bundle;
  6. import android.widget.TextView;
  7.  
  8. public class MainActivity extends Activity {
  9.  
  10. @Override
  11. public void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14.  
  15. final Typeface typeface = Typeface.createFromAsset(getAssets(),"fonts/custom_fonts.ttf");
  16. final TextView tv = (TextView) findViewById(R.id.text_view);
  17. tv.setTypeface(typeface);
  18. tv.setTextSize(60);
  19. tv.setLineSpacing(-30f,1f); // *** -30 to reduce line spacing
  20. tv.setBackgroundColor(0x280000ff);
  21. tv.setText("gggkiiikkk" + "\n" + "gikgikgik" + "\n" + "kigkigkig");
  22. }
  23. }

这导致在视图底部截断(注意底线的’g’):

似乎问题与错误的布局测量有关.如果我将TextView设置为

  1. android:layout_height="fill_parent"

它确实正确呈现:

知道怎么解决吗?如果它有帮助,我不介意有丑陋的变通方法.我也可以访问FontForge,如果需要我可以修改字体文件.

解决方法

如果填充不起作用,则边距应该完成.如果仍有问题,可以始终将行间距值应用于视图的onMeasure方法.你必须为此创建一个 custom component并扩展onMeasure.

猜你在找的Android相关文章