【问题标题】:How to remove TextView top margin?如何删除 TextView 上边距?
【发布时间】:2012-01-17 22:54:10
【问题描述】:

TextView 确实有问题。我不想在它上面有任何边距/填充。

<TextView android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/ptp_hour"
          android:textColor="@color/black"
          android:textSize="100dp"
          android:height="100dp"
          android:layout_marginTop="0dp"
          android:paddingTop="0dp"
          android:includeFontPadding="false"
          android:maxLines="1"
          android:text="10"/>

我的TextView 看起来像这样,尽管textSizeheight 设置为相同的值,但字体上方有一个空格。这让我很困扰,因为我想将另一个视图相对于字体的顶部。这个间距是否包含在字体本身中?

还有一个问题:如果我发现距顶部 20dp 和距底部 7dp 的边距在我的设备上完美运行,我是否可以相信它在其他屏幕上的行为也会类似? (这些边距用于按钮)

【问题讨论】:

  • 你能发一张你看到的图片吗?
  • 是的,请张贴图片
  • 字体似乎有顶部间距。尝试将高度保持为 100dp,但增加 textSize(102dp 或 105dp 等)。
  • @MisterSquonk 如果我增加textSize 字体是从下剪下来的。
  • @MichalChudy:看我的回答。我已经解决了你的问题。

标签: android textview margin


【解决方案1】:

我遇到了同样的问题,设置 android:includeFontPadding=false 没有帮助。我能在合理时间内找到的最佳解决方案是覆盖 TextView 的 onDraw 方法,并根据字体度量的顶部值和上升值之间的差异调整画布:

FontMetricsInt fontMetricsInt;
@Override
protected void onDraw(Canvas canvas) {
    if (adjustTopForAscent){
        if (fontMetricsInt == null){
            fontMetricsInt = new FontMetricsInt();
            getPaint().getFontMetricsInt(fontMetricsInt);
        }
        canvas.translate(0, fontMetricsInt.top - fontMetricsInt.ascent);
    }
    super.onDraw(canvas);
}

【讨论】:

  • 这几乎可以工作,在一个小字体的 TextView 中它可以完美对齐。在 74sp 字体上,没那么多。但是 +1 对于这个很好的解决方案。
【解决方案2】:

使用android:includeFontPadding="false" 在类似情况下帮助了我很多。

【讨论】:

    【解决方案3】:

    是的,默认情况下包含此空间。根据我的搜索区域,您无法删除该空间。所以你需要实现一些逻辑才能有这样的视图。

    见下图:

    这不是一个好主意,但你可以像下面的代码一样:

        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:background="#ffffff">
        <TextView android:layout_width="wrap_content"           
            android:layout_height="wrap_content"           
            android:id="@+id/ptp_hour"           
            android:textColor="@android:color/black"           
            android:textSize="100sp"
            android:lineSpacingMultiplier="0.8"
            android:scrollY="-100dp"
            android:scrollX="100dp"
            android:text="10"/>
        <TextView 
            android:text="10"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:textColor="#00FFFFFF"
            android:shadowColor="#000000"
            android:shadowDy="-50"
            android:shadowRadius="1"
            android:textSize="100dp"/>
    
    </LinearLayout>
    

    这里,第一个“10”是你的属性,第二个是我为你设置的。

    享受。 :))

    【讨论】:

    • 您可以通过将 android:lineSpacingExtra 设置为负值来实现相同的效果。但是它们都有一个问题,就是 TextView 会将字符裁剪到 g、q、p 等边界之外。
    • 我觉得很有趣。但如果我也想删除底部多余的空间,那也没用。
    • 请关注@Maurycy 的回答。这是正确的事情需要做同样的事情。
    【解决方案4】:

    您需要做的是将另一个视图相对于字体顶部放置,并在 dip 中给它一个负的android:layout_marginBottom,使其与字体顶部匹配。如果字体有边距,我认为没有更好的方法。

    【讨论】: