获得实际的窗口宽度android

时间:2013-03-06 09:20:17

标签: android

所以我试图获得手机屏幕的实际大小,以便集中一堆菜单按钮。在我的姜饼手机上,它工作正常,但在Jellybean模拟器上,这一切都错了......

WindowManager wm = (WindowManager) Assets.context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    try{
        Point size = new Point();
        display.getSize(size);
        screenHeight = size.y;
        screenWidth = size.x;
    }catch(NoSuchMethodError e){
        //API lower than 12, revert to deprecated methods
        screenWidth = display.getWidth();
        screenHeight = display.getHeight();
    }*/

但是当我绘制时,例如,在x = screenWidth / 2处的一条线,在豆荚上,它远离屏幕中间!这让我很生气,任何帮助都非常感激:)

2 个答案:

答案 0 :(得分:3)

您应该使用DisplayMetrics代替。试试这个:

DisplayMetrics metrics = Assets.context.getResources().getDisplayMetrics();
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;

答案 1 :(得分:1)

Display display =getWindowManager().getDefaultDisplay();

try{

    screenHeight = display.getHeight();;
    screenWidth = display.getWidth();
}catch(NoSuchMethodError e){
    //API lower than 12, revert to deprecated methods

}*/