开发者

Getting screen height returns a larger screen than the physical screen can display

When I get the screen size for my Android app, it returns a variabl开发者_如何转开发e which in every case is too large for the screen to display. I use the following code to get screen size.

final int windowHeight = getResources().getDisplayMetrics().heightPixels;
final int windowWidth = getResources().getDisplayMetrics().widthPixels;

Is there any way to compensate for it? Or to even fix my problem?

Also, is there a way to turn the battery/time bar off in the app? I think that might be the reason I'm having trouble, but I don't actually know. That's why I am asking.


To disable the battery/time bar, I use this in my AndroidManifest.xml file, for the <application> tag :

<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name" 
    android:theme="@android:style/Theme.NoTitleBar" 
    ...
    >


And, to get the screen's size, here's what I have in my Activity :

    Display display = getWindowManager().getDefaultDisplay(); 
    int screenWidth = display.getWidth();
    int screenHeight = display.getHeight();


Notice that you might want density points instead of pixels. If you get the dimensions in pixels here's how you convert them. But you can get the dimensions in dp too

        /*  
         * Get dps from pixel.
         */
        float scale = getResources().getDisplayMetrics().density;

        public static float dpFromPixels(int pixels) {
            float dp = (float) (pixels / scale + 0.5f);
            return dp;
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜