getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING); DisplayMetrics metric = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metric); int width = metric.widthPixels; int height = metric.heightPixels; float density = metric.density; densityDpi = metric.densityDpi;
知道 dengsityDpi之后,有利用对不同手机尺寸的layout的width and height进行适配性设置。
例如,我想要加入的图片大小是100dp。就可以这样进行设置:
final ImageView image = new ImageView(view.getContext()); double size = densityDpi/160.0*100.0; ViewGroup.LayoutParams img = new ViewGroup.LayoutParams((int)size, (int)size); image.setLayoutParams(img);
注意:LayoutParams(int, int)这里面要设置的参数单位是Pixel!