2012-02-21 140 views
5

由於android 3.0在顯示屏上有一個帶有後退和home按鈕的導航欄。現在,如果我得到的屏幕尺寸這樣android獲取其他屏幕方向的屏幕大小

ClockLwp.ScreenHeight = getWindowManager().getDefaultDisplay().getHeight(); 
ClockLwp.ScreenWidth = getWindowManager().getDefaultDisplay().getWidth(); 

getResources().getDisplayMetrics().widthPixels 
getResources().getDisplayMetrics().heightPixels 

我得到了橫向和縱向不同的值。這裏是我的意思是一個例子:

宏碁Iconia A500人像模式:800 X 1232 宏碁Iconia A500風景模式:1280 * 752

AFAIK這也適用於這樣的導航欄的所有設備。 (例如Samsung Galaxy Nexus,GalaxyTab 10.1等)

現在我的問題是,如果我處於肖像模式,我如何獲取橫向模式的值以及其他方式?

回答

1

因爲android 3沒有辦法用public api來檢索raw heigth和width。你可以通過反思。尋找getRawHeigth /寬度

+0

這不是真的原始高度是我想要的寬度。它是減去導航欄高度後的寬度和高度值,但是是其他方向的值。 – Xazen 2012-02-21 22:57:40

+0

做一些數學! – Blackbelt 2012-02-22 08:03:06

+0

我不知道如何獲得導航欄的高度。 (包含後退和主頁按鈕,不知道如何正確調用此欄)。 – Xazen 2012-02-22 10:45:02

0

這是適合我的解決方案。我有真正的屏幕大小,然後減去導航欄高度。

// get navigation bar height for landscape and portrait modes 
    Resources resources = activity.getResources(); 
    int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); 
    int resourceIdLandscape = resources.getIdentifier("navigation_bar_height_landscape", "dimen", "android"); 
    int portraitNavBarHeight = 0; 
    int landscapeNavBarHeight = 0; 

    boolean hasPermanentMenuKey = ViewConfiguration.get(activity).hasPermanentMenuKey(); 
    if (resourceId > 0 && !hasPermanentMenuKey) { 
     portraitNavBarHeight = resources.getDimensionPixelSize(resourceId); 
    } 

    if (resourceIdLandscape > 0 && !hasPermanentMenuKey) { 
     landscapeNavBarHeight = resources.getDimensionPixelSize(resourceIdLandscape); 
    } 

    // get real screen size 
    DisplayMetrics displayMetrics = new DisplayMetrics(); 
    WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE); 
    Display disp = wm.getDefaultDisplay(); 

    int API_LEVEL = android.os.Build.VERSION.SDK_INT; 
    if (API_LEVEL >= 17) { 
     disp.getRealMetrics(displayMetrics); 
    } else { 
     disp.getMetrics(displayMetrics); 
    } 

    int width = displayMetrics.widthPixels; 
    int height = displayMetrics.heightPixels; 

    // size for orientation 
    int portraitHeight; 
    int portraitWidth; 
    int landscapeHeight; 
    int landscapeWidth; 

    if(width > height) { 
     portraitHeight = width - portraitNavBarHeight; 
     portraitWidth = height; 

     landscapeHeight = height - landscapeNavBarHeight; 
     landscapeNavBarHeight = width; 

    } else { 
     portraitHeight = height - portraitNavBarHeight; 
     portraitWidth = width; 

     landscapeHeight = width - landscapeNavBarHeight; 
     landscapeNavBarHeight = height; 
    } 

如果您的應用程序狀態欄

public static int getStatusBarHeight(Context context) { 
    Resources resources = context.getResources(); 
    int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android"); 
    if (resourceId > 0) { 
     return resources.getDimensionPixelSize(resourceId); 
    } 
    return 0; 
} 
0

EDIT(2014年9月26日):我改寫我的文字包含一個完整的答案,而不僅僅是發表評論德米特里解決方案。

下面是我們使用來計算旋轉之前導航欄高度的一部分:

DisplayMetrics metrics = new DisplayMetrics(); 
mFragment.getActivity().getWindowManager().getDefaultDisplay() 
      .getMetrics(metrics); 

/* 
* We need to take system resources here to verify navigation 
* bar height. Application resources also contains navigation bar 
* height but this value is not valid for some devices e.g. Nexus 7 (2012)! 
*/ 
Resources appRes = getResources(); 
Resources sysRes = Resources.getSystem(); 
boolean landscapeMode = (metrics.widthPixels > metrics.heightPixels); 
/* Check if the device has navigation bar. There is no direct API for 
* that so we use some hacking that may not always work, 
* (see http://stackoverflow.com/questions/16092431/check-for-navigation-bar) 
* but this method should be enough for most devices. 
* TODO: Find a more consistent way. For our needs it is currently enough as we 
* support limited set of devices. 
*/ 
boolean hasNavigationBar = !ViewConfiguration.get(getContext()).hasPermanentMenuKey(); 
int navBarHeight = 0; 
int navBarHeightRotated = 0; 
if (hasNavigationBar) { 
    int barHeightDimId = 0; 
    int barHeightDimIdRotated = 0; 
    if (landscapeMode) { 
     barHeightDimId = sysRes.getIdentifier(
       "navigation_bar_height_landscape", "dimen", "android"); 
     barHeightDimIdRotated = sysRes.getIdentifier(
       "navigation_bar_height", "dimen", "android"); 
    } else { 
     barHeightDimIdRotated = sysRes.getIdentifier(
       "navigation_bar_height_landscape", "dimen", "android"); 
     barHeightDimId = sysRes.getIdentifier("navigation_bar_height", 
       "dimen", "android"); 
    } 
    if (barHeightDimId != 0) { 
     navBarHeight = appRes.getDimensionPixelSize(barHeightDimId); 
    } 
    if (barHeightDimIdRotated != 0) { 
     navBarHeightRotated = appRes 
       .getDimensionPixelSize(barHeightDimIdRotated); 
    } 
    NCLogs.i("MyApp", String.format("Android navigation bar height: %d (current), %d (after rotation)", navBarHeight, navBarHeightRotated)); 
} 

那麼我們就可以旋轉後計算出我們的觀點的寬度和高度:

int realScreenHeight = metrics.heightPixels + navBarHeight; 

// We expect to not have decor views near the left or right borders 
int realScreenWidth = metrics.widthPixels; 

/* 
* Height of decor views: system status bar + any application addtional elements + 
* + system navigation bar 
*/ 
int decorHeightRotated = realScreenHeight - getHeight() - navBarHeight + navBarHeightRotated; 

int viewWidthRotated = realScreenHeight; 
int viewHeightRotated = (realScreenWidth - decorHeightRotated); 
NCLogs.i("MyApp", String.format("Width after rotation: %d, Height after rotation: %d", viewWidthRotated, viewHeightRotated));