2014-02-08 28 views
3

我只需要檢測,如果導航欄位於屏幕的右側,如下面的圖像看到的權利。由於檢測,如果導航欄位於屏幕

screenshot with navbar to the right

+0

的可能重複[檢測的Android導航欄方向( http://stackoverflow.com/questions/21057035/detect-android-navigation-bar-orientation) – NPike

+0

請如何更改導航欄的當前方向位置? – Benny

回答

1

導航欄將只在屏幕的右側,如果它是在橫向模式。因此,要檢測這種情況,使用getResources().getConfiguration().orientation這樣的:

String orientation = getResources().getConfiguration().orientation; 
if(orientation.equals("ORIENTATION_LANDSCAPE"){ 
    // screen in landscape, do what you want to do 
} 
+0

謝謝你的回答,但我知道這一點。事情是,在一些設備上(大部分),NavBar不是在右邊,而是在底部。 –

+1

@ImmaWake爲什麼你需要檢測這個(只是想知道)?至於...我不知道你會如何檢測它。 – hichris123

+0

我好幾個星期前發佈了這個問題> http://stackoverflow.com/questions/21058087/android-disable-overlay-of-actionbar-and-navigationbar-for-flag-translucent並沒有得到迴應,幫助。所以我的工作是添加填充的頂部和底部我的佈局匹配狀態欄和導航欄寬度 –

2

下面的代碼片段可以幫助:

private boolean isNavigationBarRightOfContent(){ 
    Rect outRect = new Rect(); 
    ViewGroup decor = (ViewGroup) mActivity.getWindow().getDecorView(); 
    decor.getWindowVisibleDisplayFrame(outRect); 
    DisplayMetrics dm = getResources().getDisplayMetrics(); 
    return dm.widthPixels == outRect.bottom; 
} 
0

試試這個:

// retrieve the position of the DecorView 
    Rect visibleFrame = new Rect(); 
    getWindow().getDecorView().getWindowVisibleDisplayFrame(visibleFrame); 

    DisplayMetrics dm = getResources().getDisplayMetrics(); 
    // check if the DecorView takes the whole screen vertically or horizontally 
    boolean isRightOfContent = dm.heightPixels == visibleFrame.bottom; 
    boolean isBelowContent = dm.widthPixels == visibleFrame.right;