{"id":972,"date":"2019-03-09T00:23:00","date_gmt":"2019-03-08T16:23:00","guid":{"rendered":"https:\/\/www.ray650128.com\/wordpress\/?p=972"},"modified":"2021-07-22T11:47:27","modified_gmt":"2021-07-22T03:47:27","slug":"android%e9%97%9c%e6%96%bc%e7%b3%bb%e7%b5%b1%e7%b8%ae%e6%94%be%e6%af%94%e4%be%8b%e9%80%a0%e6%88%90%e7%95%ab%e9%9d%a2%e8%b7%91%e7%89%88%e7%9a%84%e4%b8%80%e5%80%8b%e8%a7%a3%e6%b3%95","status":"publish","type":"post","link":"https:\/\/blog.ray650128.com\/?p=972","title":{"rendered":"[Android]\u95dc\u65bc\u7cfb\u7d71\u7e2e\u653e\u6bd4\u4f8b\u9020\u6210\u756b\u9762\u8dd1\u7248\u7684\u4e00\u500b\u89e3\u6cd5"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5f88\u591a\u958b\u767c\u8005\u901a\u5e38\u90fd\u6703\u9047\u5230\u4f7f\u7528\u8005\u8abf\u6574\u7cfb\u7d71\u4e2d\u7684\u756b\u9762\u7e2e\u653e\u6bd4\u4f8b\uff0c\u5c0e\u81f4APP\u756b\u9762\u8dd1\u7248\u7684\u554f\u984c\u767c\u751f\u5427\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7531\u65bc\u667a\u6167\u578b\u624b\u6a5f\u7684\u4f7f\u7528\u8005\u773e\u591a\uff0c\u4e14\u958b\u767c\u8005\u7121\u6cd5\u9650\u5236\u4f7f\u7528\u8005\u7684\u884c\u70ba\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u56e0\u6b64\u8dd1\u7248\u6703\u662f\u6211\u5011\u8eab\u70ba\u958b\u767c\u8005\uff0c\u6703\u9047\u5230\u7684\u96e3\u984c\u4e4b\u4e00\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9019\u88e1\u6703\u91dd\u5c0d\u4f7f\u7528\u8005\u8abf\u6574\u7cfb\u7d71\u5b57\u9ad4\u5927\u5c0f\u3001\u6216\u662f\u8abf\u6574\u87a2\u5e55\u7e2e\u653e\u6bd4\u4f8b\uff0c\u9020\u6210\u8dd1\u7248\u7684\u554f\u984c\uff0c\u4f7f\u7528\u4e00\u500b\u7c21\u55ae\u7684\u89e3\u6c7a\u65b9\u5f0f\u4f86\u8655\u7406<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5148\u653e\u4e0a\u7a0b\u5f0f\u78bc\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">object ScaleUtils {\n    fun attachBaseContext(context: Context): Context {\n        return if (Build.VERSION.<em>SDK_INT <\/em>&gt;= Build.VERSION_CODES.<em>N<\/em>) { \/\/ 8.0\u9700\u8981\u4f7f\u7528createConfigurationContext\u8655\u7406\n            updateResources(context)\n        } else {\n            context\n        }\n    }\n\n    @TargetApi(Build.VERSION_CODES.<em>N<\/em>)\n    private fun updateResources(context: Context): Context {\n        val displayMetrics = context.<em>resources<\/em>.<em>displayMetrics\n        <\/em>val snap = 20\n        val exactDpi = (displayMetrics.xdpi + displayMetrics.ydpi) \/ 2\n        val dpi = displayMetrics.densityDpi\n\n        val resources = context.<em>resources\n        <\/em>val configuration = resources.<em>configuration\n\n        <\/em>if (dpi - exactDpi &gt; snap) {\n            val targetDpi = (<em>ceil<\/em>(exactDpi \/ snap) * snap).toInt()\n            displayMetrics.densityDpi = targetDpi\n            configuration.densityDpi = targetDpi\n            displayMetrics.setTo(displayMetrics)\n            configuration.setTo(configuration)\n        }\n\n        if(configuration.fontScale != 1.0f) {\n            configuration.fontScale = 1.0f\n            configuration.densityDpi\n        }\n\n        return context.createConfigurationContext(configuration)\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u9019\u500b\u7a0b\u5f0f\u78bc\u662f\u4e00\u500b\u5de5\u5177\u985e\u5225\uff0c\u4e3b\u8981\u662f\u6aa2\u67e5\u7cfb\u7d71\u7684\u6587\u5b57\u5927\u5c0f\u3001\u756b\u9762\u7e2e\u653e\u6bd4\u4f8b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u624b\u6a5f\u7684\u7e2e\u653e\u6bd4\u4f8b\u6709\u767c\u751f\u6539\u8b8a\uff0c\u5247\u8b93APP\u4f7f\u7528\u7cfb\u7d71\u9810\u8a2d\u7684\u7e2e\u653e\u6bd4\u4f8b\u7e6a\u88fd\u756b\u9762\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u81f3\u65bc\u5982\u4f55\u4f7f\u7528\uff0c\u5247\u5728Activity\u4e2d\u8986\u5bebattachBaseContext\u65b9\u6cd5\u5373\u53ef\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">override fun attachBaseContext(newBase: Context?) {\n    if (newBase == null) {\n        super.attachBaseContext(newBase)\n    } else {\n        val newContext = ScaleUtils.attachBaseContext(newBase)\n        super.attachBaseContext(newContext)\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f88\u591a\u958b\u767c\u8005\u901a\u5e38\u90fd\u6703\u9047\u5230\u4f7f\u7528\u8005\u8abf\u6574\u7cfb\u7d71\u4e2d\u7684\u756b\u9762\u7e2e\u653e\u6bd4\u4f8b\uff0c\u5c0e\u81f4APP\u756b\u9762\u8dd1\u7248\u7684\u554f\u984c\u767c\u751f\u5427\uff1f \u7531\u65bc\u667a\u6167\u578b\u624b\u6a5f\u7684\u4f7f\u7528\u8005 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[10,19,32],"class_list":["post-972","post","type-post","status-publish","format-standard","hentry","category-2","tag-android-app","tag-kotlin","tag-32"],"_links":{"self":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts\/972","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=972"}],"version-history":[{"count":3,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts\/972\/revisions"}],"predecessor-version":[{"id":975,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts\/972\/revisions\/975"}],"wp:attachment":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}