检测导航栏可见性

时间:2017-09-17 13:12:05

标签: java android galaxy

我正在尝试检查导航栏的可见性。在三星Galaxy S8上,我可以切换导航栏的可见性。

我尝试了很多不同的方法来检查可见性,但它们都不适用于Galaxy S8。

一些例子:(无论是显示还是隐藏,它们都会返回相同的值)

ViewConfiguration.get(getBaseContext()).hasPermanentMenuKey()始终返回false KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK)始终返回false KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME)始终返回true

即使通过计算导航栏高度(How do I get the height and width of the Android Navigation Bar programmatically?),它也无法正常工作。

2 个答案:

答案 0 :(得分:0)

也许这对某人有用。用户可以隐藏导航栏,因此检测可见性的最佳方法是订阅此事件。它有效。

angular.json

答案 1 :(得分:0)

由于撰写本文时唯一的答案是Kotlin,因此这里是Java的替代方案:

import android.content.res.Resources;
import android.support.v4.view.OnApplyWindowInsetsListener;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.WindowInsetsCompat;
import android.view.View;

public class NavigationBar {

    final int BOTTOM = 0;
    final int RIGHT = 1;
    final int LEFT = 2;
    final int NONE = 3;
    private int LOCATION = NONE;

    private View view;

    NavigationBar(View view) {
        this.view = view;
    }

    int getNavBarLocation() {
        ViewCompat.setOnApplyWindowInsetsListener(view, new OnApplyWindowInsetsListener() {
            public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
                if (insets.getSystemWindowInsetBottom() != 0)
                    LOCATION = BOTTOM;
                else if (insets.getSystemWindowInsetRight() != 0)
                    LOCATION = RIGHT;
                else if (insets.getSystemWindowInsetLeft() != 0)
                    LOCATION = LEFT;
                else
                    LOCATION = NONE;
                return insets;
            }
        });
        return LOCATION;
    }

    int getNavBarHeight() {
        Resources resources = view.getResources();
        int resourceId = resources.getIdentifier(
                "navigation_bar_height", "dimen", "android");
        if (resourceId > 0)
            return resources.getDimensionPixelSize(resourceId);
        return 0;
    }
}

我还介绍了如何获得高度,因为通常这是下一步。

在您的活动中,然后您将使用基于视图的引用来调用方法:

    NavigationBar navigationBar = new NavigationBar(snackbarLayout);
    if (navigationBar.getNavBarLocation() == navigationBar.BOTTOM) {
        FrameLayout.LayoutParams parentParams =
                (FrameLayout.LayoutParams) snackbarLayout.getLayoutParams();
        parentParams.setMargins(0, 0, 0, 0 - navigationBar.getNavBarHeight());
        snackbarLayout.setLayoutParams(parentParams);
    }

(此示例基于SnackBar边距不一致的常见问题)