Android 获取屏幕宽高
WindowManager m = getActivity().getWindowManager(); DisplayMetrics metric = new DisplayMetrics(); m.getDefaultDisplay().getMetrics(metric); int width = metric.widthPixels; // 屏幕宽度(像素) int height = metric.heightPixels; // 屏幕高度(像素) float density = metric.density; // 屏幕密度(0.75 / 1.0 / 1.5) int densityDpi = metric.densityDpi; // 屏幕密度DPI(120 / 160 / 240)
//将一个view设为屏幕宽度 Button b = new Button(getActivity()); layout.addView(b, new LayoutParams( width, LinearLayout.LayoutParams.WRAP_CONTENT));
需要获取多分辨率支持,在AndroidManifest.xml文件中,加入supports-screens节点
<supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:resizeable="true" android:anyDensity="true" />