您的位置:首页 > 移动开发 > Android开发

android字体大小多屏幕适配

2016-11-23 16:03 295 查看
在android中,我们有两种情况可以设置字体大小。一种是在xml页面中,另一种是在Java代码中。 

[html] view
plain copy

    <TextView  

        android:id="@+id/hello_word"  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content"  

        android:textSize="14sp"  

        android:text="@string/hello_world" />  

其中android:textSize字段就是设置字体大小。谷歌官方推荐在设置字体大小时候,用sp为单位。

在android中系统预设了3种默认字体样式供选择,即大、中、小号字体,默认的为小号字体。

[html] view
plain copy

<TextView  

    android:id="@+id/textView1"  

    android:layout_width="wrap_content"  

    android:layout_height="wrap_content"  

    android:text="Small Text"  

    android:textAppearance="?android:attr/textAppearanceSmall" />  

  

<TextView  

    android:id="@+id/textView2"  

    android:layout_width="wrap_content"  

    android:layout_height="wrap_content"  

    android:text="Medium Text"  

    android:textAppearance="?android:attr/textAppearanceMedium" />  

  

<TextView  

    android:id="@+id/textView3"  

    android:layout_width="wrap_content"  

    android:layout_height="wrap_content"  

    android:text="Large Text"  

    android:textAppearance="?android:attr/textAppearanceLarge" />  

另一种是在java代码中设置。

[java] view
plain copy

TextView tv;  

  

@Override  

protected void onCreate(Bundle savedInstanceState) {  

    super.onCreate(savedInstanceState);  

    setContentView(R.layout.activity_main);  

    tv = (TextView) this.findViewById(R.id.hello_word);  

    tv.setTextSize(12);  

}  

在代码中设置的时候需要注意一下,setTextSize方法中的参数,默认单位是px(像素)。但是在android设备中,各种型号的屏幕,适配器来就成了问题。因此android提供了第二种设置字体大小的方法。

[html] view
plain copy

tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);  

但是在一些工具api中,比如在Achartengine中,提供设置字体的方法都是像素为单位的。并没有提供设置单位的方法来设置。

[html] view
plain copy

<pre name="code" class="java">        renderer.setAxisTitleTextSize(12);  

        renderer.setChartTitleTextSize(16);  

        renderer.setLabelsTextSize(10);  

        renderer.setLegendTextSize(12);  


这就需要我们自己进行转换。我们仿照android方法,看看大神们是如何实现的

[java] view
plain copy

@android.view.RemotableViewMethod  

public void setTextSize(float size) {  

    setTextSize(TypedValue.COMPLEX_UNIT_SP, size);  

}  

我们看到,其实setTextSize(int size)方法就是调用了setTextSize(int unit,int size)方法。默认单位是sp。再接着看下去:

[java] view
plain copy

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()));  

  }  

系统是先通过Context拿到了Resources,在通过Resources拿到了DisplayMetrics,DisplayMetrics是一个对象,官方解释是The resource's current display metrics. 保存屏幕的一些数据。

[java] view
plain copy

<span style="white-space:pre">    </span>TypedValue.applyDimension(unit, size, r.getDisplayMetrics())  

又是什么呢?我们进去看看

[java] view
plain copy

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;  

}  

我们可以理解为,TypedValue中有个静态方法,功能是能把一个float数值,转化成相对应屏幕上显示的px,dip,sp,pt,in,mm相应的长度返回。所以可以仿照代码,我们自己写一个转换方法

[java] view
plain copy

<span style="white-space:pre">        </span>renderer.setAxisTitleTextSize(changeTextSize(12));  

        renderer.setChartTitleTextSize(changeTextSize(16));  

        renderer.setLabelsTextSize(changeTextSize(10));  

        renderer.setLegendTextSize(changeTextSize(12));  

[java] view
plain copy

private int changeTextSize(int size) {  

    Resources r = getResources();  

    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,  

            size, r.getDisplayMetrics());  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息