{"id":1261,"date":"2023-03-18T09:51:30","date_gmt":"2023-03-18T01:51:30","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1261"},"modified":"2023-04-29T09:26:24","modified_gmt":"2023-04-29T01:26:24","slug":"android-content-is-blocked-by-the-bottom-virtual-navigation-bar","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/18\/android-content-is-blocked-by-the-bottom-virtual-navigation-bar\/","title":{"rendered":"Android\u5185\u5bb9\u88ab\u5e95\u90e8\u865a\u62df\u5bfc\u822a\u680f\u906e\u6321\u89e3\u51b3"},"content":{"rendered":"<h2>BaseActivity\u4e2d\u8bbe\u7f6e<\/h2>\n<pre><code class=\"language-java\">@Override\nprotected void onCreate(@Nullable Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    \/\/\u5e95\u90e8\u865a\u62df\u5bfc\u822a\u680f\u9002\u914d\n    if (StatusBarUtil.hasNavigationBarShow(this)) {\n        getWindow().getDecorView().findViewById(android.R.id.content).setPadding(0, 0, 0, StatusBarUtil.getNavigationBarHeight(this));\n    }\n}<\/code><\/pre>\n<p><!-- more --><\/p>\n<h2>StatusBarUtil\u5de5\u5177\u7c7b<\/h2>\n<pre><code class=\"language-java\">public static boolean hasNavigationBarShow(Activity activity) {\n    if (Build.VERSION.SDK_INT &lt; Build.VERSION_CODES.JELLY_BEAN_MR1) {\n        return false;\n    }\n    WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);\n    Display display = wm.getDefaultDisplay();\n    DisplayMetrics outMetrics = new DisplayMetrics();\n    \/\/\u83b7\u53d6\u6574\u4e2a\u5c4f\u5e55\u7684\u9ad8\u5ea6\n    display.getRealMetrics(outMetrics);\n    int heightPixels = outMetrics.heightPixels;\n    int widthPixels = outMetrics.widthPixels;\n    \/\/\u83b7\u53d6\u5185\u5bb9\u5c55\u793a\u90e8\u5206\u7684\u9ad8\u5ea6\n    outMetrics = new DisplayMetrics();\n    display.getMetrics(outMetrics);\n    int heightPixelsContent = outMetrics.heightPixels;\n    int widthPixelsContent = outMetrics.widthPixels;\n    int h = heightPixels - heightPixelsContent;\n    int w = widthPixels - widthPixelsContent;\n    return w &gt; 0 || h &gt; 0;  \/\/\u7ad6\u5c4f\u548c\u6a2a\u5c4f\u4e24\u79cd\u60c5\u51b5\n}\n\n\/**\n * \u83b7\u53d6\u5bfc\u822a\u680f\u9ad8\u5ea6\n *\n * @param context\n * @return\n *\/\npublic static int getNavigationBarHeight(Context context) {\n    return getSystemComponentDimen(context, &quot;navigation_bar_height&quot;);\n}\n\npublic static int getSystemComponentDimen(Context context, String dimenName) {\n    \/\/ \u53cd\u5c04\u624b\u673a\u8fd0\u884c\u7684\u7c7b\uff1aandroid.R.dimen.status_bar_height.\n    int statusHeight = -1;\n    try {\n        Class&lt;?&gt; clazz = Class.forName(&quot;com.android.internal.R$dimen&quot;);\n        Object object = clazz.newInstance();\n        String heightStr = clazz.getField(dimenName).get(object).toString();\n        int height = Integer.parseInt(heightStr);\n        \/\/dp-&gt;px\n        statusHeight = context.getResources().getDimensionPixelSize(height);\n    } catch (Exception e) {\n        e.printStackTrace();\n    }\n    return statusHeight;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>BaseActivity\u4e2d\u8bbe\u7f6e @Override protected void onCreate(@Null [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[],"class_list":["post-1261","post","type-post","status-publish","format-standard","hentry","category-android-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1261","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1261"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1261\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}