How to get screen resolution in Android Honeycomb?

后端 未结 10 1836
情深已故
情深已故 2020-12-24 09:07

I want to get real resolution of screen on Android Honeycomb.

Here\'s my code

Display display = getWindowManager().getDefaultDisplay();
int w = displ         


        
10条回答
  •  孤独总比滥情好
    2020-12-24 09:56

    The answer you are getting, as properly deduced is because of the status bar. All you have to do is get rid of the status bar before your window display initiates. And then you can reset the status bar before you set the content view of the activity.

    The reason to do it this way is that getting rid of the status bar affects your view drawing unless you handle all measure, layout and draw dynamically. Doing this is the middle of your runtime will cause the status bar to disappear, and then reappear if you want it to, resulting in confusion from users.

    To Hide the StatusBar:

    In your onCreate():

    final WindowManager.LayoutParams attrs = getWindow().getAttributes();
    //Add the flag to the Window Attributes
    attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
    getWindow().setAttributes(attrs);
    //Disassociate Display from the Activity
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    

    Now your Default Display should work correctly

    Still in your onCreate():

    Display display = getWindowManager().getDefaultDisplay();
    int w = display.getWidth();
    int h = display.getHeight();
    

    Now before you set the Content

    Again, in your onCreate():

    final WindowManager.LayoutParams attrs = getWindow().getAttributes();
    //Show the statubar
    attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().setAttributes(attrs);
    // Reassociate.
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    

    Finally:

    setContentView(r.layout.myView);
    

    The previous code segments will work almost anywhere, actually. I'm just thinking about your user experience. Feel free to place them whereever, of course. These are functional segments pulled from one of my projects. I've seen techniques similar in some Home Launchers as well. Note: depending on the Android version, you might have to do the status bar stuff in onWindowAttached(). If you do, make sure you still call super.onWindowAttached().

    Another Technique: Of course, if you want to do this anyway, you could always set the attribute of the activity this way in your manifest.

    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    

提交回复
热议问题