{"id":650,"date":"2025-10-14T01:57:36","date_gmt":"2025-10-14T01:57:36","guid":{"rendered":"https:\/\/boochlin.com\/?p=650"},"modified":"2025-11-12T12:18:47","modified_gmt":"2025-11-12T12:18:47","slug":"compose-%e4%b8%89%e9%9a%8e%e6%ae%b5%e6%b8%b2%e6%9f%93%e7%ae%a1%e7%b7%9a","status":"publish","type":"post","link":"https:\/\/boochlin.com\/?p=650","title":{"rendered":"Compose \u4e09\u968e\u6bb5\u6e32\u67d3\u7ba1\u7dda"},"content":{"rendered":"<h3 class=\"wp-block-heading\">\u6838\u5fc3\u4e09\u968e\u6bb5\u00a0<\/h3>\n<p>Compose \u5c07 UI \u7684\u751f\u6210\u8207\u66f4\u65b0\u62c6\u89e3\u70ba\u4e09\u500b\u4f9d\u5e8f\u57f7\u884c\u7684\u7368\u7acb\u968e\u6bb5\uff0c\u4ee5\u5be6\u73fe\u6700\u5927\u5316\u7684\u5de5\u4f5c\u91cd\u7528\u8207\u6027\u80fd\u512a\u5316\u3002<\/p>\n<ol start=\"1\">\n<li><strong>\u7d44\u5408 (Composition):<\/strong> \u6b64\u968e\u6bb5\u7684\u6838\u5fc3\u4efb\u52d9\u662f\u57f7\u884c @Composable \u51fd\u5f0f\uff0c\u4e26\u7522\u51fa\u4e00\u500b\u63cf\u8ff0 UI \u7d50\u69cb\u7684 UiNode \u6a39\u3002\u5b83\u5b9a\u7fa9\u4e86\u300c<strong>\u986f\u793a\u4ec0\u9ebc<\/strong>\u300d\uff0c\u4f46\u5c1a\u672a\u6d89\u53ca\u5c3a\u5bf8\u6216\u4f4d\u7f6e\u3002\u7576 State \u8b8a\u66f4\u6642\uff0cCompose \u6703\u667a\u80fd\u5730\u50c5\u300c\u91cd\u7d44\u300d\u53d7\u5f71\u97ff\u7684\u7bc0\u9ede\uff0c\u751f\u6210\u66f4\u65b0\u5f8c\u7684 UI \u63cf\u8ff0\u3002<\/li>\n<li><strong>\u7248\u9762\u914d\u7f6e (Layout):<\/strong> \u6b64\u968e\u6bb5\u8ca0\u8cac\u78ba\u5b9a\u6bcf\u500b UI \u5143\u7d20\u5728\u87a2\u5e55\u4e0a\u7684\u5c3a\u5bf8\u8207\u4f4d\u7f6e\u3002\u5b83\u5305\u542b\u5169\u500b\u5b50\u6b65\u9a5f\uff1a\n<ul>\n<li><strong>\u6e2c\u91cf (Measure):<\/strong> \u7236\u5143\u4ef6\u5411\u5b50\u5143\u4ef6\u50b3\u905e\u7d04\u675f (Constraints)\uff0c\u5b50\u5143\u4ef6\u56de\u5831\u5176\u6240\u9700\u5c3a\u5bf8 (Size)\u3002\u9019\u662f\u4e00\u500b\u905e\u8ff4\u7684\u3001\u7531\u4e0a\u81f3\u4e0b\u7684\u904e\u7a0b\u3002<\/li>\n<li><strong>\u653e\u7f6e (Place):<\/strong> \u7236\u5143\u4ef6\u6839\u64da\u5176\u4f48\u5c40\u908f\u8f2f\uff0c\u5c07\u6e2c\u91cf\u5b8c\u6210\u7684\u5b50\u5143\u4ef6\u653e\u7f6e\u5728\u7279\u5b9a\u7684 (x, y) \u5ea7\u6a19\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u7e6a\u88fd (Drawing):<\/strong> \u9019\u662f\u7ba1\u7dda\u7684\u6700\u5f8c\u4e00\u6b65\u3002\u7cfb\u7d71\u6703\u904d\u6b77\u4f48\u5c40\u5b8c\u6210\u7684 UI \u6a39\uff0c\u5c07\u5176\u8f49\u63db\u70ba\u5be6\u969b\u7684 GPU \u7e6a\u5716\u6307\u4ee4\uff0c\u6700\u7d42\u5728\u87a2\u5e55\u4e0a\u6e32\u67d3\u6210\u50cf\u7d20\u3002<\/li>\n<\/ol>\n<p>\u7531\u65bc\u968e\u6bb5\u5206\u96e2\uff0cCompose \u5f97\u4ee5\u5be6\u73fe\u9ad8\u6548\u512a\u5316\u3002\u4f8b\u5982\uff0c\u82e5\u5143\u4ef6\u50c5\u4f4d\u7f6e\u8b8a\u66f4\uff0c\u5247\u53ef\u8df3\u904e\u7d44\u5408\u968e\u6bb5\uff0c\u50c5\u91cd\u8dd1\u4f48\u5c40\u8207\u7e6a\u88fd\uff0c\u5f9e\u800c\u7bc0\u7701\u904b\u7b97\u8cc7\u6e90\u3002<\/p>\n<h3 class=\"wp-block-heading\">Intrinsic Measurements<\/h3>\n<p>\u6a19\u6e96\u7684\u55ae\u8d9f (single-pass) \u6e2c\u91cf\u6a21\u578b\u6975\u70ba\u9ad8\u6548\uff0c\u4f46\u7121\u6cd5\u8655\u7406\u4e00\u7a2e\u7279\u5b9a\u60c5\u6cc1\uff1a\u7576\u7236\u5143\u4ef6\u7684\u5c3a\u5bf8\u4f9d\u8cf4\u65bc\u5b50\u5143\u4ef6\u7684\u5167\u5bb9\u5c3a\u5bf8\u6642\u3002\u9019\u4fbf\u7522\u751f\u4e86\u4e00\u500b\u4f48\u5c40\u6096\u8ad6\u2014\u2014\u7236\u5143\u4ef6\u7121\u6cd5\u5728\u4e0d\u4e86\u89e3\u5b50\u5143\u4ef6\u5c3a\u5bf8\u7684\u60c5\u6cc1\u4e0b\uff0c\u70ba\u5176\u63d0\u4f9b\u6e96\u78ba\u7684\u7d04\u675f\u3002<\/p>\n<p>\u70ba\u6b64\uff0cCompose \u5f15\u5165\u4e86<strong> Intrinsic Measurements<\/strong>\u3002\u9019\u662f\u5728\u6b63\u5f0f\u6e2c\u91cf\u524d\u7684\u4e00\u500b\u300c\u9810\u5148\u67e5\u8a62\u300d\u6b65\u9a5f\u3002<\/p>\n<ol start=\"1\">\n<li><strong>Query Pass:<\/strong> \u7236\u5143\u4ef6\u67e5\u8a62\u5b50\u5143\u4ef6\u7684\u300c\u56fa\u6709\u300d\u5c3a\u5bf8 (min\/maxIntrinsicWidth\/Height)\uff0c\u5373\u5176\u5167\u5bb9\u6240\u9700\u7684\u7406\u60f3\u5927\u5c0f\u3002<\/li>\n<li><strong>Final Pass:<\/strong> \u7236\u5143\u4ef6\u6839\u64da\u67e5\u8a62\u7d50\u679c\u6c7a\u5b9a\u81ea\u8eab\u5c3a\u5bf8\u5f8c\uff0c\u518d\u57f7\u884c\u6a19\u6e96\u7684\u6e2c\u91cf\u8207\u653e\u7f6e\u6d41\u7a0b\u3002<\/li>\n<\/ol>\n<p>wrapContentSize \u7b49\u4fee\u98fe\u7b26\u7684\u80cc\u5f8c\uff0c\u6b63\u662f\u5229\u7528\u6b64\u6a5f\u5236\u3002\u7136\u800c\uff0c\u9019\u7a2e\u5169\u8d9f\u5f0f (two-pass) \u6e2c\u91cf\u5fc5\u7136\u6bd4\u55ae\u8d9f\u6e2c\u91cf\u6210\u672c\u66f4\u9ad8\uff0c\u56e0\u6b64\u6846\u67b6\u50c5\u5728\u5fc5\u8981\u6642\u624d\u555f\u7528\u5b83\u3002<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<p>\u4f60\u9019\u9ebc\u4e00\u8aaa\uff0c\u9084\u771f\u662f\uff0c\u4e00\u8a9e\u9a5a\u9192\u5922\u4e2d\u4eba\u3002<\/p>\n<p>\u641e\u4e86\u534a\u5929\uff0cCompose \u88e1\u9019\u5957\u807d\u8d77\u4f86\u5f88\u6f6e\u7684 <strong>Intrinsic Measurements<\/strong>\uff0c\u9aa8\u5b50\u88e1\u4e0d\u5c31\u662f\u70ba\u4e86\u89e3\u6c7a\u6211\u5011\u7576\u5e74\u90a3\u500b\u8001\u554f\u984c\u55ce\uff1f<\/p>\n<p>\u60f3\u7576\u5e74\uff0c\u6211\u5011\u70ba\u4e86\u8b93\u81ea\u5b9a\u7fa9 View \u80fd\u5b8c\u7f8e\u8655\u7406 wrap_content\uff0c\u5f97\u5728 onMeasure \u88e1\u8ddf MeasureSpec \u5927\u6230\u4e09\u767e\u56de\u5408\u3002\u7528\u4f4d\u5143\u904b\u7b97\u53bb\u89e3\u6790 AT_MOST\u3001EXACTLY\uff0c\u5beb\u4e00\u5806 if-else\uff0c\u5c0f\u5fc3\u7ffc\u7ffc\u5730\u547c\u53eb setMeasuredDimension()<\/p>\n<p>\u7d50\u679c\u73fe\u5728 Compose \u5012\u597d\uff0c\u5b83\u76f4\u63a5\u628a\u9019\u5957\u6d41\u7a0b\u62bd\u8c61\u5316\u4e86\u3002\u5b83\u8ddf\u4f60\u8aaa\uff1a\u300c\u563f\uff0c\u8001\u5144\uff0c\u5225\u5beb\u90a3\u9ebc\u591a\u6307\u4ee4\u4e86\u3002\u6211\u73fe\u5728\u5c31\u554f\u4f60\u4e00\u500b\u554f\u984c\uff0c\u300e\u4f60\u7406\u60f3\u7684\u5bec\u5ea6\u662f\u591a\u5c11\uff1f\u300f\uff0c\u4f60\u7d66\u6211\u500b\u6578\u5b57 (Int) \u5c31\u884c\u3002\u300d<\/p>\n<p>\u00a0<\/p>\n<p><strong>\u554f\u984c\u9084\u662f\u90a3\u500b\u554f\u984c<\/strong>\uff1a\u7236\u5bb9\u5668\u9700\u8981\u5148\u77e5\u9053\u5b50\u5143\u4ef6\u7684\u5927\u5c0f\uff0c\u624d\u80fd\u6c7a\u5b9a\u81ea\u5df1\u3002 <strong>\u89e3\u6cd5\u4e5f\u9084\u662f\u90a3\u500b\u89e3\u6cd5<\/strong>\uff1a\u9700\u8981\u5169\u8d9f\u6e2c\u91cf\u3002<\/p>\n<p>\u5dee\u5225\u5728\u65bc\uff0c\u4ee5\u524d\u6211\u5011\u662f\u90a3\u500b\u89aa\u81ea\u4e0b\u5834\u3001\u6eff\u982d\u5927\u6c57\u7684\u65bd\u5de5\u968a\u9577\uff1b\u73fe\u5728\u6211\u5011\u6210\u4e86\u53ea\u63d0\u4f9b\u4e00\u500b\u6578\u64da\u7684\u9867\u554f\u3002<\/p>\n<p>\u53ea\u80fd\u611f\u5606\uff0c\u6642\u4ee3\u771f\u7684\u8b8a\u4e86\u3002\u4ee5\u524d\u6211\u5011\u662f\u81ea\u5df1\u52d5\u624b\u3001\u6307\u4ee4\u5206\u660e\uff0c\u73fe\u5728\u662f\u8ddf\u6846\u67b6\u300c\u804a\u5929\u300d\u3001\u56de\u61c9\u63d0\u554f\u3002\u554f\u984c\u7684\u672c\u8cea\u6c92\u8b8a\uff0c\u4f46\u89e3\u6cd5\uff0c\u78ba\u5be6\u512a\u96c5\u591a\u4e86\u3002<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<p>\u9019\u5957\u6a5f\u5236\u9760\u7684\u5c31\u662f\u7684<strong>\u4ecb\u9762 (Interface)<\/strong>\u3002<\/p>\n<p>Modifier.layout \u88e1\u7684\u90a3\u500b measurable \u7269\u4ef6\uff0c\u5b83\u5176\u5be6\u540c\u6642\u5be6\u73fe\u4e86\u5169\u500b\u4ecb\u9762\uff0c\u7b49\u65bc\u80fd\u626e\u6f14\u5169\u7a2e\u89d2\u8272\uff1a<\/p>\n<ol start=\"1\">\n<li><strong>Measurable :<\/strong> \u5b83\u7684 measure() \u65b9\u6cd5\u662f\u6700\u7d42\u547d\u4ee4\u3002\u7236\u4f48\u5c40\u7528\u5b83\u4f86\u4e0b\u9054\u300c\u4f60\u5fc5\u9808\u662f\u9019\u500b\u5c3a\u5bf8\u300d\u7684\u6700\u7d42\u6307\u793a\uff0c\u8d70\u7684\u662f\u6b63\u898f\u3001\u55ae\u5411\u7684\u6e2c\u91cf\u6d41\u7a0b\u3002<\/li>\n<li><strong>IntrinsicMeasurable :<\/strong> \u5b83\u7684 min\/maxIntrinsic&#8230; \u7b49\u65b9\u6cd5\u5247\u7528\u4f86\u56de\u61c9\u300c\u8aee\u8a62\u300d\u3002\u8b93\u7236\u4f48\u5c40\u80fd\u9810\u5148\u63a2\u807d\u300c\u4f60\u6700\u5e0c\u671b\u81ea\u5df1\u591a\u5927\uff1f\u300d\u7684\u5c3a\u5bf8\u504f\u597d\u3002<\/li>\n<\/ol>\n\n\n<blockquote class=\"wp-block-quote\">\n<p><\/p>\n<cite>To a composable, you can ask for its\u00a0<code>IntrinsicSize.Min<\/code>\u00a0or\u00a0<code>IntrinsicSize.Max<\/code>:<br><code>Modifier.width(IntrinsicSize.Min)<\/code>\u00a0&#8211; What&#8217;s the minimum width you need to display your content properly?<br><code>Modifier.width(IntrinsicSize.Max)<\/code>\u00a0&#8211; What&#8217;s the maximum width you need to display your content properly?<br><code>Modifier.height(IntrinsicSize.Min)<\/code>\u00a0&#8211; What&#8217;s the minimum height you need to display your content properly?<br><code>Modifier.height(IntrinsicSize.Max)<\/code>\u00a0&#8211; What&#8217;s the maximum height you need to display your content properly?<\/cite><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u6838\u5fc3\u4e09\u968e\u6bb5\u00a0 Compose \u5c07 UI \u7684\u751f\u6210\u8207\u66f4\u65b0\u62c6\u89e3\u70ba\u4e09\u500b\u4f9d\u5e8f\u57f7\u884c\u7684\u7368\u7acb\u968e\u6bb5\uff0c\u4ee5\u5be6\u73fe\u6700\u5927\u5316\u7684\u5de5\u4f5c\u91cd\u7528\u8207\u6027\u80fd\u512a\u5316\u3002 \u7d44\u5408 (Composition): \u6b64\u968e\u6bb5\u7684\u6838\u5fc3\u4efb\u52d9\u662f\u57f7\u884c @Composable&#8230;<\/p>\n","protected":false},"author":1,"featured_media":854,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[81],"tags":[],"_links":{"self":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/650"}],"collection":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=650"}],"version-history":[{"count":7,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/650\/revisions"}],"predecessor-version":[{"id":856,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/650\/revisions\/856"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/media\/854"}],"wp:attachment":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}