2012-02-21 47 views
5

vì Android 3.0 có thanh điều hướng có nút quay lại và nút trang chủ trên màn hình. Bây giờ nếu tôi nhận được kích thước màn hình như thế nàyandroid nhận kích thước màn hình của hướng màn hình khác

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

này

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

tôi nhận được giá trị khác nhau cho phong cảnh và chân dung. Dưới đây là một ví dụ tôi muốn nói gì:

Acer Iconia A500 chế độ chân dung: 800 x 1232 Acer Iconia A500 chế độ phong cảnh: 1280 x 752

AFAIK này đi cho tất cả các thiết bị với một thanh điều hướng như vậy. (ví dụ: Samsung Galaxy Nexus, GalaxyTab 10.1, v.v.)

Bây giờ câu hỏi của tôi là, nếu tôi đang ở chế độ dọc, làm thế nào tôi có thể nhận được các giá trị của chế độ ngang và cách khác?

Trả lời

1

kể từ android 3 không có cách nào với api công khai để truy xuất phần thô và chiều rộng thô. Bạn có thể trải qua sự phản chiếu. Hãy tìm getRawHeigth/Width

+0

Nó không phải thực sự là chiều cao thô chiều rộng tôi muốn. Đó là các giá trị chiều rộng và chiều cao sau khi trừ chiều cao thanh điều hướng, nhưng theo hướng khác. – Xazen

+0

thực hiện một số phép tính !!! – Blackbelt

+0

Tôi không biết làm thế nào tôi có thể nhận được chiều cao của thanh điều hướng. (có chứa nút quay lại và nút trang chủ, không chắc chắn cách thanh này được gọi chính xác.) – Xazen

0

Đây là giải pháp phù hợp với tôi. Tôi đã có kích thước màn hình thực và sau đó trừ chiều cao thanh điều hướng.

// 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; 
    } 

Nếu ứng dụng của bạn có thanh trạng thái

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 (26 tháng 9 năm 2014): Tôi thay đổi công văn bản của tôi có chứa một câu trả lời đầy đủ và không chỉ bình luận để giải pháp Dmitri.

Đây là phần mà chúng tôi đang sử dụng để tính toán chiều cao thanh điều hướng trước khi luân chuyển:

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)); 
} 

sau đó chúng ta có thể tính toán chiều rộng tầm nhìn của chúng tôi và chiều cao sau khi luân chuyển:

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)); 
Các vấn đề liên quan