如何根据屏幕大小设置字体大小

时间:2014-01-18 12:03:33

标签: android android-fonts

我在我的值中添加了每个屏幕大小的字体大小 对于普通屏幕:<dimen name="fontsize">22sp</dimen> 对于小屏幕:<dimen name="fontsize">19sp</dimen> ...

但是当我有两个正常的屏幕尺寸时,一个xhdpi和另一个hdpi我必须添加另一个值文件夹: 对于normal-xhdpi屏幕:<dimen name="fontsize">22sp</dimen> 对于普通hdpi屏幕:<dimen name="fontsize">15sp</dimen>

但我知道sp单位根据屏幕dpi的不同而有所不同,为什么我有这个错误?如何在所有屏幕上都有所需的字体大小?

我想知道的是:

  

属        与比例无关的像素 - 这与dp单位类似,但它也可以根据用户的字体大小首选项进行缩放。推荐你   指定字体大小时使用此单位,因此将调整它们   屏幕密度和用户偏好。

那为什么不工作呢?我为每个屏幕尺寸小,正常,大和xlarge放置了一个valuse文件夹,我认为sp会根据dpi进行更改,所以不需要添加dpi ..但那不能正常工作吗?

1 个答案:

答案 0 :(得分:1)

使用这个utillity类:

com.cutompackage;
    import android.content.Context;
    import android.graphics.Paint;
    import android.util.AttributeSet;
    import android.util.TypedValue;
    import android.widget.TextView;
    /**
     * Utillity class which is changing font size of text view in app in order to fit to given dimensions
     * @author Darko
     *
     */
    public class FontFitTextView extends TextView {

        public FontFitTextView(Context context) {
            super(context);
            initialise();
        }

        public FontFitTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            initialise();
        }

        private void initialise() {
            mTestPaint = new Paint();
            mTestPaint.set(this.getPaint());
            // max size defaults to the initially specified text size unless it is
            // too small
        }

        /*
         * Re size the font so the specified text fits in the text box assuming the
         * text box is the specified width.
         */
        private void refitText(String text, int textWidth) {
            if (textWidth <= 0)
                return;
            int targetWidth = textWidth - this.getPaddingLeft()
                    - this.getPaddingRight();
            float hi = 100;
            float lo = 2;
            final float threshold = 0.5f; // How close we have to be

            mTestPaint.set(this.getPaint());

            while ((hi - lo) > threshold) {
                float size = (hi + lo) / 2;
                mTestPaint.setTextSize(size);
                if (mTestPaint.measureText(text) >= targetWidth)
                    hi = size; // too big
                else
                    lo = size; // too small
            }
            // Use lo so that we undershoot rather than overshoot
            if (this.getTextSize() > lo) {
                this.setTextSize(TypedValue.COMPLEX_UNIT_PX, lo);
            }
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
            int height = getMeasuredHeight();
            refitText(this.getText().toString(), parentWidth);
            this.setMeasuredDimension(parentWidth, height);
        }

        @Override
        protected void onTextChanged(final CharSequence text, final int start,
                final int before, final int after) {
            refitText(text.toString(), this.getWidth());
        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            if (w != oldw) {
                refitText(this.getText().toString(), w);
            }
        }

        // Attributes
        private Paint mTestPaint;
    }

然后只需在xml中设置:

 <com.cutompackage.FontFitTextView
                android:id="@+id/headerText"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="@color/text_color"
                android:textSize="22sp" />

编辑: 它会自动更改字体大小以适合TextView的给定尺寸(高度和宽度),现在您只需要正确设置尺寸。

希望有所帮助

相关问题