Android TextView 設定行間距的問題 (setLineSpacing ,lineSpacingExtra, lineSpacingMultiplier)

來源:互聯網
上載者:User

標籤:android   style   class   blog   code   ext   

最近的項目中,需要用到自訂控制項。有個組件要擷取繪製文本的高度,翻開TextView 的源碼,有個方法 getLineHeight()

    public int getLineHeight() {        return FastMath.round(mTextPaint.getFontMetricsInt(null) * mSpacingMult + mSpacingAdd);    }
getLineHeight 的注釋如下:

/*** @return the height of one standard line in pixels. Note that markup* within the text can cause individual lines to be taller or shorter* than this height, and the layout may contain additional first-* or last-line padding.*/返回一行的高度。注意標記內的文本可能高於或低於這個高度,布局可能包含額外的第一行或最後一行填充的部分。
我們只是擷取一行文字的高度,不計算間距。
所以使用 FastMath.round( mTextPaint.getFontMetricsInt(null) ) 即可。

在網上也找到了一些通過其他辦法,擷取文本高度方式。在測試之後,發現使用上述方法,取到的高度最精確。測試的代碼就不貼了。。

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.