码迷,mamicode.com
首页 > 编程语言 > 详细

在Java代码上设置TextView字体大小

时间:2014-08-13 00:45:04      阅读:338      评论:0      收藏:0      [点我收藏+]

标签: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); 

 

在Java代码上设置TextView字体大小,布布扣,bubuko.com

在Java代码上设置TextView字体大小

标签:des   android   style   blog   color   java   os   io   

原文地址:http://www.cnblogs.com/veins/p/3908677.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!