由于我的要求是覆盖现有的textView from findViewById(getResources().getIdentifier("xxx", "id", "android"));
,因此我不能简单地尝试onDraw()
其他答案。
但是我只是想出解决问题的正确方法,这是Layout Inspector的最终结果:

因为我想要的只是删除顶部空格,所以我不必选择其他字体来删除底部空格。
这是修复它的关键代码:
Typeface mfont = Typeface.createFromAsset(getResources().getAssets(), "fonts/myCustomFont.otf");
myTextView.setTypeface(mfont);
myTextView.setPadding(0, 0, 0, 0);
myTextView.setIncludeFontPadding(false);
第一个键是设置自定义字体“ fonts / myCustomFont.otf”,该字体在底部但不在顶部,您可以通过打开otf文件轻松找到该字体,然后在android Studio中单击任意字体:

如您所见,底部的光标有额外的间距,但顶部没有,因此可以解决我的问题。
第二个关键是您不能简单地跳过任何代码,否则可能无法正常工作。因此,您会发现有些人认为答案有效,而另一些人认为答案无效。
让我们说明一下如果删除其中之一会发生什么情况。
没有setTypeface(mfont);
:

没有setPadding(0, 0, 0, 0);
:

没有setIncludeFontPadding(false);
:

如果没有其中的3个(即原始版本):
