2014-01-18 259 views
0

我在值增加了字体大小,每个屏幕尺寸 正常屏幕:<dimen name="fontsize">22sp</dimen> 为小屏幕:<dimen name="fontsize">19sp</dimen> ...如何设置字体的大小取决于屏幕尺寸

但是当我有两个正常屏幕尺寸一个xhdpi和其他HDPI我不得不添加另一个值文件夹: 正常-xhdpi屏幕:<dimen name="fontsize">22sp</dimen> 正常,华电国际的屏幕:<dimen name="fontsize">15sp</dimen>

,但我知道的是,SP单元根据画面的不同为什么我有这个呃ror,我如何在所有屏幕上获得所需的字体大小?

我想知道的是:

SP 规模无关的像素 - 这就像DP单位,但它也受到了用户的字体大小偏爱缩放。建议您在指定字体大小时使用本机,因此将根据屏幕密度和用户偏好调整 。

那么为什么不工作呢?我为每个屏幕尺寸放置了一个小的,正常的,大的和xlarge的文件夹,并且我认为sp会根据dpi而改变,所以不需要添加dpi ..但这不起作用吗?

回答

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现在你只需要正确设置尺寸。

希望它有帮助

+0

这是干什么的? –

+0

好吧,我只是编辑答案,试试吧 –

+0

好的,谢谢你会试试看。 –

相关问题