簡體   English   中英

如何根據屏幕尺寸設置字體大小

[英]How to set the font size depending on the screen size

我在值中添加了普通屏幕的每種屏幕尺寸的字體大小: <dimen name="fontsize">22sp</dimen>用於小屏幕: <dimen name="fontsize">19sp</dimen> ...

但是當我有兩個普通屏幕尺寸時,一個xhdpi,另一個hdpi我必須添加另一個值文件夾:對於normal-xhdpi屏幕: <dimen name="fontsize">22sp</dimen> dimen <dimen name="fontsize">22sp</dimen>對於常規hdpi屏幕: <dimen name="fontsize">15sp</dimen>

但是我知道sp單位因屏幕dpi而異,為什么會出現此錯誤,以及如何在所有屏幕上都具有所需的字體大小?

我想知道的是:

sp與縮放無關的像素-類似於dp單位,但它也通過用戶的字體大小首選項進行縮放。 建議您在指定字體大小時使用本機,以便針對屏幕密度和用戶喜好進行調整。

那為什么不起作用呢? 我為每個屏幕尺寸(小,中,大和xlarge)放置了一個valuse文件夾,我認為sp將根據dpi進行更改,因此無需添加dpi ..但這不起作用嗎?

使用此實用程序類:

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給定尺寸(高度和寬度),現在您只需要正確設置尺寸即可。

希望能幫助到你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM