android 適配dpi框架在程式碼中設定TextView setTextSize時的使用
阿新 • • 發佈:2018-12-16
我這裡用的是阿杜的dpi適配框架,DisplayUtil是常見的單位轉換工具類,可自行搜尋
TextView .setTextSize()方法傳入的是sp值
檢視原始碼
/** * Set the default text size to the given value, interpreted as "scaled * pixel" units. This size is adjusted based on the current density and * user font size preference. * * <p>Note: if this TextView has the auto-size feature enabled than this function is no-op. * * @param size The scaled pixel size. * * @attr ref android.R.styleable#TextView_textSize */
翻譯:
/**
*將預設文字大小設定為給定值,解釋為“縮放”。
*畫素“單位”。根據電流密度調整該尺寸。
*使用者字型大小優先。
*> P>注意:如果此TabVIEW具有自動大小特性,則此函式為NO-OP。
*@ PARAM大小縮放畫素大小。
*@ Atf.REF Android
*/
也就是傳入的應該是sp值
所以應該如此使用自己適配後的sp值
tv.setTextSize(DisplayUtil.px2sp(TApplication.getAppResources().getDimension(R.dimen.sp_10)));