在Java代碼上設定TextView字型大小

來源:互聯網
上載者:User

標籤:des   android   style   blog   color   java   os   io   

部分源碼:

    /**     * Set the default text size to a given unit and value.  See {@link     * TypedValue} for the possible dimension units.     *     * @param unit The desired dimension unit.     * @param size The desired size in the given units.     *     * @attr ref android.R.styleable#TextView_textSize     */    public void setTextSize(int unit, float size) {        Context c = getContext();        Resources r;        if (c == null)            r = Resources.getSystem();        else            r = c.getResources();        setRawTextSize(TypedValue.applyDimension(            unit, size, r.getDisplayMetrics()));    }    private void setRawTextSize(float size) {        if (size != mTextPaint.getTextSize()) {            mTextPaint.setTextSize(size);            if (mLayout != null) {                nullLayouts();                requestLayout();                invalidate();            }        }    }    /**     * Converts an unpacked complex data value holding a dimension to its final floating      * point value. The two parameters <var>unit</var> and <var>value</var>     * are as in {@link #TYPE_DIMENSION}.     *       * @param unit The unit to convert from.     * @param value The value to apply the unit to.     * @param metrics Current display metrics to use in the conversion --      *                supplies display density and scaling information.     *      * @return The complex floating point value multiplied by the appropriate      * metrics depending on its unit.      */    public static float applyDimension(int unit, float value,                                       DisplayMetrics metrics)    {        switch (unit) {        case COMPLEX_UNIT_PX:            return value;        case COMPLEX_UNIT_DIP:            return value * metrics.density;        case COMPLEX_UNIT_SP:            return value * metrics.scaledDensity;        case COMPLEX_UNIT_PT:            return value * metrics.xdpi * (1.0f/72);        case COMPLEX_UNIT_IN:            return value * metrics.xdpi;        case COMPLEX_UNIT_MM:            return value * metrics.xdpi * (1.0f/25.4f);        }        return 0;    }

1.預設情況下:

<dimen name="text_size">20sp</dimen>
float size = mContext.getResources().getDimension(R.dimen.text_size);textView.setTextSize(size); 

此時,size擷取的值為20,但textView設定的字型大小為:setRawTextSize(20 * metrics.scaledDensity);

 

2.設定字型大小為20:

float size = mContext.getResources().getDimension(R.dimen.text_size);child.setTextSize(TypedValue.COMPLEX_UNIT_PX, size); 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.