android不同机型上界面适配问题

android中长度有:dp(或者dipdeviceindependentpixels)一种基于屏幕密度的抽象单位。在每英寸160点的显示器上,1dp=1px。不同设备有不同的显示效果,这个和设备硬件有关,布局时尽量使用单位dp;sp(ScaledPixels)主要用于字体显示,与刻度无关的一种像素,与dp类似,,但是可以根据用户的字体大小首选项进行缩放;px:表示屏幕实际的象素。TextView的方法setTextSize使用sp作为字号单位。Point的方法setTextSize使用px为单位。下面是转换放方法:

public static int px2sp(Context context, float pxValue) {final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;return (int) (pxValue / fontScale + 0.5f); } public static int px2dip(Context context, float pxValue) {final float scale = context.getResources().getDisplayMetrics().density;return (int) (pxValue / scale + 0.5f); } public static int dip2px(Context context, float dpValue) {final float scale = context.getResources().getDisplayMetrics().density;return (int) (dpValue * scale + 0.5f); }

和属于我们的风景。一起吃早餐,

android不同机型上界面适配问题

相关文章:

你感兴趣的文章:

标签云: