{"id":47,"date":"2024-04-10T21:43:29","date_gmt":"2024-04-10T13:43:29","guid":{"rendered":"https:\/\/www.loml.cc\/?p=47"},"modified":"2024-04-10T21:43:30","modified_gmt":"2024-04-10T13:43:30","slug":"android-%e5%88%a4%e6%96%ad%e8%99%9a%e6%8b%9f%e5%af%bc%e8%88%aa%e6%a0%8f%e6%98%af%e5%90%a6%e5%ad%98%e5%9c%a8","status":"publish","type":"post","link":"https:\/\/www.loml.cc\/index.php\/2024\/04\/10\/android-%e5%88%a4%e6%96%ad%e8%99%9a%e6%8b%9f%e5%af%bc%e8%88%aa%e6%a0%8f%e6%98%af%e5%90%a6%e5%ad%98%e5%9c%a8\/","title":{"rendered":"Android \u5224\u65ad\u865a\u62df\u5bfc\u822a\u680f\u662f\u5426\u5b58\u5728"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5f53\u6211\u4eec\u9700\u8981\u786e\u5b9a\u8bbe\u5907\u662f\u5426\u5b58\u5728\u865a\u62df\u5bfc\u822a\u680f\u65f6\uff0c\u53ef\u4ee5\u501f\u52a9 Android \u7cfb\u7edf\u5c42\u9762\u7684\u6280\u672f\u6765\u5b9e\u73b0\u3002\u865a\u62df\u5bfc\u822a\u680f\u672c\u8d28\u4e0a\u4e5f\u662f\u4e00\u4e2a View\uff0c\u5728\u5e03\u5c40\u4e2d\u7ed8\u5236\u5e76\u663e\u793a\u5728\u7a97\u53e3\u5e03\u5c40\u4e2d\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5206\u6790\u5e03\u5c40\u5c42\u7ea7\u627e\u5230\u4ee3\u8868\u865a\u62df\u5bfc\u822a\u680f\u7684 View\uff0c\u5e76\u9a8c\u8bc1\u5176\u662f\u5426\u5b58\u5728\u6765\u5224\u65ad\u865a\u62df\u5bfc\u822a\u680f\u7684\u5b58\u5728\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Android Studio \u63d0\u4f9b\u7684 Layout Inspector \u5de5\u5177\u6765\u5206\u6790\u5e94\u7528\u7684\u5e03\u5c40\u5c42\u7ea7\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u865a\u62df\u5bfc\u822a\u680f\u7684 View \u662f DecorView \u7684\u5b50 View\uff08\u5728 Android 5.0 \u4ee5\u4e0a\u7684\u7248\u672c\u4e2d\u662f\u8fd9\u6837\uff09\u3002\u5728 DecorView \u4e2d\u627e\u5230\u4ee3\u8868\u865a\u62df\u5bfc\u822a\u680f\u7684 View \u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u7ee7\u7eed\u9a8c\u8bc1\u5176\u662f\u5426\u5b58\u5728\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f\u901a\u8fc7\u4ee3\u7801\u5b9e\u73b0\u8fd9\u4e00\u8fc7\u7a0b\u7684\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private static final String NAVIGATION = \"navigationBarBackground\";\n\n\/**\n * \u5224\u65ad\u8bbe\u5907\u662f\u5426\u5b58\u5728\u5bfc\u822a\u680f\u3002\n * \u6ce8\u610f\uff1a\u6b64\u65b9\u6cd5\u9700\u8981\u5728\u89c6\u56fe\u5b8c\u5168\u7ed8\u5236\u51fa\u6765\u4e4b\u540e\u8c03\u7528\uff0c\u5426\u5219\u65e0\u6cd5\u51c6\u786e\u5224\u65ad\u3002\n * \u4f8b\u5982\uff0c\u5728 onWindowFocusChanged() \u65b9\u6cd5\u4e2d\u8c03\u7528\u53ef\u4ee5\u5f97\u5230\u6b63\u786e\u7684\u7ed3\u679c\u3002\n *\n * @param activity \u5f53\u524d\u6d3b\u52a8\n * @return \u5982\u679c\u5b58\u5728\u5bfc\u822a\u680f\u5219\u8fd4\u56de true\uff0c\u5426\u5219\u8fd4\u56de false\n *\/\npublic static boolean isNavigationBarExist(@NonNull Activity activity) {\n    \/\/ \u83b7\u53d6\u7a97\u53e3\u7684 DecorView\n    ViewGroup vp = (ViewGroup) activity.getWindow().getDecorView();\n    \n    \/\/ \u904d\u5386 DecorView \u7684\u5b50 View\uff0c\u5bfb\u627e\u5bfc\u822a\u680f\u7684 View\n    if (vp != null) {\n        for (int i = 0; i &lt; vp.getChildCount(); i++) {\n            \/\/ \u83b7\u53d6\u5f53\u524d\u5b50 View\n            View childView = vp.getChildAt(i);\n            \n            \/\/ \u5224\u65ad\u5f53\u524d\u5b50 View \u662f\u5426\u662f\u5bfc\u822a\u680f\u7684\u80cc\u666f View\n            if (childView.getId() != View.NO_ID &amp;&amp; NAVIGATION.equals(activity.getResources().getResourceEntryName(childView.getId()))) {\n                return true; \/\/ \u5982\u679c\u662f\u5bfc\u822a\u680f\u80cc\u666f View\uff0c\u5219\u8868\u793a\u5b58\u5728\u5bfc\u822a\u680f\n            }\n        }\n    }\n    return false; \/\/ \u672a\u627e\u5230\u5bfc\u822a\u680f\u80cc\u666f View\uff0c\u8868\u793a\u4e0d\u5b58\u5728\u5bfc\u822a\u680f\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd8\u6709\u53e6\u4e00\u79cd\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * \u5224\u65ad\u8bbe\u5907\u662f\u5426\u5b58\u5728\u5bfc\u822a\u680f\uff0c\u5e76\u56de\u8c03\u5bfc\u822a\u680f\u72b6\u6001\u3002\n * \u6ce8\u610f\uff1a\u6b64\u65b9\u6cd5\u9700\u8981\u5728\u89c6\u56fe\u5b8c\u5168\u7ed8\u5236\u51fa\u6765\u4e4b\u540e\u8c03\u7528\uff0c\u5426\u5219\u65e0\u6cd5\u51c6\u786e\u5224\u65ad\u3002\n *\n * @param activity              \u5f53\u524d\u6d3b\u52a8\n * @param onNavigationStateListener \u5bfc\u822a\u680f\u72b6\u6001\u76d1\u542c\u5668\n *\/\npublic static void isNavigationBarExist(Activity activity, final OnNavigationStateListener onNavigationStateListener) {\n    if (activity == null) {\n        return;\n    }\n    \n    \/\/ \u83b7\u53d6\u5bfc\u822a\u680f\u9ad8\u5ea6\n    final int height = getNavigationHeight(activity);\n\n    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {\n        \/\/ \u8bbe\u7f6e\u7a97\u53e3 DecorView \u7684 OnApplyWindowInsetsListener\uff0c\u76d1\u542c\u7a97\u53e3\u53d8\u5316\n        activity.getWindow().getDecorView().setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {\n            @Override\n            public WindowInsets onApplyWindowInsets(View v, WindowInsets windowInsets) {\n                boolean isShowing = false;\n                int b = 0;\n                \/\/ \u5224\u65ad\u7a97\u53e3\u53d8\u5316\u662f\u5426\u6d89\u53ca\u5230\u7cfb\u7edf\u7a97\u53e3\u5e95\u90e8\u7684\u63d2\u5165\uff08\u5373\u5bfc\u822a\u680f\uff09\n                if (windowInsets != null) {\n                    b = windowInsets.getSystemWindowInsetBottom();\n                    isShowing = (b == height);\n                }\n                \/\/ \u56de\u8c03\u5bfc\u822a\u680f\u72b6\u6001\n                if (onNavigationStateListener != null &amp;&amp; b &lt;= height) {\n                    onNavigationStateListener.onNavigationState(isShowing, b);\n                }\n                return windowInsets;\n            }\n        });\n    } \n}\n\n\/**\n * \u83b7\u53d6\u5bfc\u822a\u680f\u7684\u9ad8\u5ea6\u3002\n *\n * @param activity \u4e0a\u4e0b\u6587\n * @return \u5bfc\u822a\u680f\u9ad8\u5ea6\uff0c\u5355\u4f4d\u4e3a\u50cf\u7d20\n *\/\npublic static int getNavigationHeight(Context activity) {\n    if (activity == null) {\n        return 0;\n    }\n    \/\/ \u83b7\u53d6\u7cfb\u7edf\u8d44\u6e90\n    Resources resources = activity.getResources();\n    \/\/ \u83b7\u53d6\u5bfc\u822a\u680f\u9ad8\u5ea6\u8d44\u6e90\u6807\u8bc6\u7b26\n    int resourceId = resources.getIdentifier(\"navigation_bar_height\",\n            \"dimen\", \"android\");\n    int height = 0;\n    if (resourceId > 0) {\n        \/\/ \u83b7\u53d6\u5bfc\u822a\u680f\u9ad8\u5ea6\n        height = resources.getDimensionPixelSize(resourceId);\n    }\n    return height;\n}\n\n\/**\n * \u5bfc\u822a\u680f\u72b6\u6001\u76d1\u542c\u5668\u63a5\u53e3\u3002\n *\/\npublic interface OnNavigationStateListener {\n    \/**\n     * \u5bfc\u822a\u680f\u72b6\u6001\u56de\u8c03\u65b9\u6cd5\u3002\n     *\n     * @param isShowing \u5bfc\u822a\u680f\u662f\u5426\u663e\u793a\n     * @param height    \u5bfc\u822a\u680f\u9ad8\u5ea6\n     *\/\n    void onNavigationState(boolean isShowing, int height);\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u6211\u4eec\u9700\u8981\u786e\u5b9a\u8bbe\u5907\u662f\u5426\u5b58\u5728\u865a\u62df\u5bfc\u822a\u680f\u65f6\uff0c\u53ef\u4ee5\u501f\u52a9 Android \u7cfb\u7edf\u5c42\u9762\u7684\u6280\u672f\u6765\u5b9e\u73b0\u3002\u865a\u62df\u5bfc\u822a\u680f\u672c\u8d28\u4e0a\u4e5f\u662f\u4e00 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-47","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.loml.cc\/index.php\/wp-json\/wp\/v2\/posts\/47","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.loml.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.loml.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.loml.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.loml.cc\/index.php\/wp-json\/wp\/v2\/comments?post=47"}],"version-history":[{"count":1,"href":"https:\/\/www.loml.cc\/index.php\/wp-json\/wp\/v2\/posts\/47\/revisions"}],"predecessor-version":[{"id":48,"href":"https:\/\/www.loml.cc\/index.php\/wp-json\/wp\/v2\/posts\/47\/revisions\/48"}],"wp:attachment":[{"href":"https:\/\/www.loml.cc\/index.php\/wp-json\/wp\/v2\/media?parent=47"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.loml.cc\/index.php\/wp-json\/wp\/v2\/categories?post=47"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.loml.cc\/index.php\/wp-json\/wp\/v2\/tags?post=47"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}