老风格,废话不多说了,直接给大家贴android获取屏幕宽高的代码了。
主要代码:
package com.km.screeninfo; import android.os.Bundle; import android.support.v.app.Fragment; import android.support.v.app.FragmentActivity; import android.util.DisplayMetrics; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit(); } } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { private TextView tvModel; private TextView tvScreenHeight; private TextView tvScreenHeight; private TextView tvScreenHeight; private TextView tvScreenWidth; private TextView tvScreenWidth; private TextView tvScreenWidth; private TextView tvScreenDensity; private TextView tvScreenDensityDPI; private TextView tvScreenDensity; private TextView tvScreenDensityDPI; private TextView tvScreenWidth; private TextView tvScreenHeight; public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); tvModel = (TextView) rootView.findViewById(R.id.tv_model); tvScreenHeight = (TextView) rootView.findViewById(R.id.tv_screen_height); tvScreenHeight = (TextView) rootView.findViewById(R.id.tv_screen_height); tvScreenHeight = (TextView) rootView.findViewById(R.id.tv_screen_height); tvScreenHeight = (TextView) rootView.findViewById(R.id.tv_screen_height); tvScreenWidth = (TextView) rootView.findViewById(R.id.tv_screen_width); tvScreenWidth = (TextView) rootView.findViewById(R.id.tv_screen_width); tvScreenWidth = (TextView) rootView.findViewById(R.id.tv_screen_width); tvScreenWidth = (TextView) rootView.findViewById(R.id.tv_screen_width); tvScreenDensity = (TextView) rootView.findViewById(R.id.tv_screen_density); tvScreenDensityDPI = (TextView) rootView.findViewById(R.id.tv_screen_densityDPI); tvScreenDensity = (TextView) rootView.findViewById(R.id.tv_screen_density); tvScreenDensityDPI = (TextView) rootView.findViewById(R.id.tv_screen_densityDPI); tvModel.setText(android.os.Build.MODEL); // 获取屏幕密度(方法) int screenWidth = getActivity().getWindowManager().getDefaultDisplay().getWidth(); // 屏幕宽(像素,如:px) int screenHeight = getActivity().getWindowManager().getDefaultDisplay().getHeight(); // 屏幕高(像素,如:p) tvScreenHeight.setText(screenHeight + " px"); tvScreenWidth.setText(screenWidth + " px"); // 获取屏幕密度(方法) DisplayMetrics dm = getResources().getDisplayMetrics(); float density = dm.density; // 屏幕密度(像素比例:./././.) int densityDPI = dm.densityDpi; // 屏幕密度(每寸像素:///) float xdpi = dm.xdpi; float ydpi = dm.ydpi; int screenWidth = dm.widthPixels; // 屏幕宽(像素,如:px) int screenHeight = dm.heightPixels; // 屏幕高(像素,如:px) tvScreenHeight.setText(screenHeight + " px"); tvScreenWidth.setText(screenWidth + " px"); tvScreenDensity.setText(density + ""); tvScreenDensityDPI.setText(densityDPI + ""); // 获取屏幕密度(方法) DisplayMetrics dm = new DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm); density = dm.density; // 屏幕密度(像素比例:./././.) densityDPI = dm.densityDpi; // 屏幕密度(每寸像素:///) xdpi = dm.xdpi; ydpi = dm.ydpi; tvScreenDensity.setText(density + ""); tvScreenDensityDPI.setText(densityDPI + ""); int screenWidth = dm.widthPixels; // 屏幕宽(px,如:px) int screenHeight = dm.heightPixels; // 屏幕高(px,如:px) tvScreenHeight.setText(screenHeight + " px"); tvScreenWidth.setText(screenWidth + " px"); float screenWidthDip = (dm.widthPixels / density); // 屏幕宽(dip,如:dip) float screenHeightDip = (dm.heightPixels / density); // 屏幕宽(dip,如:dip) tvScreenHeight.setText(screenHeightDip + " dip"); tvScreenWidth.setText(screenWidthDip + " dip"); return rootView; } } }
activity_main.xml:
ps:在 Activity 中获得屏幕的宽和高
int srceenW = this.getWindowManager().getDefaultDisplay().getWidth(); int screenH = this.getWindowManager().getDefaultDisplay().getHeight();
如果需要适应不同分辨率的屏幕,则按照以下方式操作:
准备多套不同的图片:
分别放hdpi、mdpi、ldp文件夹中;
摆放控件的时候要得到屏幕宽高度,然后在程序中设定控件的百分乘屏幕的宽和高。