android字体大小多屏幕适配

android爱好者 落木沙 原创,转载请注明源地址http://blog.csdn.net/luomusha/article/details/41245591。谢谢。

在android中,我们有两种情况可以设置字体大小。一种是在xml页面中,另一种是在java代码中。 

    <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种默认字体样式供选择,即大、中、小号字体,默认的为小号字体。

    <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代码中设置。

	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提供了第二种设置字体大小的方法。

		tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);

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

<pre name="code" class="java">		renderer.setAxisTitleTextSize(12);
		renderer.setChartTitleTextSize(16);
		renderer.setLabelsTextSize(10);
		renderer.setLegendTextSize(12);

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

    @android.view.RemotableViewMethod
    public void setTextSize(float size) {
        setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
    }
我们看到,其实setTextSize(int size)方法就是调用了setTextSize(int unit,int size)方法。默认单位是sp。再接着看下去:
  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. 保存屏幕的一些数据。

<span style="white-space:pre">	</span>TypedValue.applyDimension(unit, size, r.getDisplayMetrics())
又是什么呢?我们进去看看
    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相应的长度返回。所以可以仿照代码,我们自己写一个转换方法
<span style="white-space:pre">		</span>renderer.setAxisTitleTextSize(changeTextSize(12));
		renderer.setChartTitleTextSize(changeTextSize(16));
		renderer.setLabelsTextSize(changeTextSize(10));
		renderer.setLegendTextSize(changeTextSize(12));

	private int changeTextSize(int size) {
		Resources r = getResources();
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
				size, r.getDisplayMetrics());
	}

这样就可以做到在设备上字体大小保持一直啦。么么哒。




  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值