The following describes how to obtain screen-related information in Android.
1. screen size, source code:
Displaymetrics metrics = new displaymetrics (); getwindowmanager (). getdefadisplay display (). getmetrics (metrics); int screenwidth = metrics. widthpixels; // screen width int screenheight = metrics. heightpixels; // screen height
This code can be inserted into the oncreate () function of the activity.
2. Get the system @ hide attribute using the Java reflection mechanism
For example, obtain the status bar height.
Code in the source code program:
height= getResources().getDimensionPixelSize(com.android.internal.R.dimen.status_bar_height);
Get code through SDK:
class c = Class.forName("com.android.internal.R$dimen");Object obj = c.newInstance();Field field = c.getField("status_bar_height");int x = Integer.parseInt(field.get(obj).toString());int y = getResources().getDimensionPixelSize(x);
Obtain the title bar and Status Bar Height:
Rect rect = new Rect();Window win = this.getWindow();win.getDecorView().getWindowVisibleDisplayFrame(rect);int statusBarHeight = rect.top;int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop();int titleBarHeight = contentViewTop - Variable.statusBarHeight;
// Statusbarheight indicates the height of the status bar and titlebarheight indicates the height of the title bar.
In addition, this Code cannot be inserted into the oncreate () and onresume () Functions of the activity. Otherwise, both values are 0. My practice is to insert it into the onpause () function and get two correct values successfully.