Gao Kuan in the onWindowFocusChanged method to get the phone screen

label Android Developmentmobile phoneScreen size
418 people read comment(0) Collection report

Development, we often have to get the phone screen high wide data. Usually we will write a method such as getScreen (); and then call the getScreen () method in the Oncreate () (). We may get to the result that the width is 0. This is why? The reason lies in:

In the Activity life cycle, onCreate, onStart, and onResume are not really visible on the screen.VISIBLEPoint of time, realVISIBLEThe time point is when the onWindowFocusChanged () function is executed, this time the user can really interact with the application, and this function is called before the user's operation is a little limited. So we have to get the correct access to the phone screen is high, it is to be implemented in the onWindowFocusChanged () method.

Void onWindowFocusChanged public (hasFocus Boolean) {
Int (width=getResources).GetDisplayMetrics (.WidthPixels); / / screen width
Int (hight=getResources).GetDisplayMetrics (.HeightPixels); / / screen height
Super.onWindowFocusChanged (hasFocus);

step on
Guess you're looking for
View comments
* the above user comments only represent their personal views, does not represent the views or position of the CSDN website
    personal data
    • visit51325 times
    • Integral:Two thousand three hundred and twenty-six
    • Grade
    • Rank:9595th name
    • original187
    • Reproduced:37
    • Translation:1
    • Comments:13
    Classification of articles
    Latest comments