{"id":1036,"date":"2023-07-27T14:52:53","date_gmt":"2023-07-27T06:52:53","guid":{"rendered":"https:\/\/www.yimenyun.cn\/emen\/?p=1036"},"modified":"2023-07-27T14:52:53","modified_gmt":"2023-07-27T06:52:53","slug":"app%e5%bc%80%e5%8f%91style%e6%a0%87%e7%ad%be%e9%9c%80%e8%a6%81%e6%b3%a8%e6%84%8f%e5%93%aa%e4%ba%9b%e7%bb%86%e8%8a%82%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.yimenyun.cn\/emen\/1036\/","title":{"rendered":"app\u5f00\u53d1style\u6807\u7b7e\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u7ec6\u8282\uff1f"},"content":{"rendered":"

\u5728 App \u5f00\u53d1\u4e2d\uff0c\u6837\u5f0f\u662f\u4e00\u79cd\u91cd\u8981\u7684\u5143\u7d20\uff0c\u5b83\u8ba9\u89c6\u89c9\u8868\u73b0\u5c42\u66f4\u52a0\u7b26\u5408\u8bbe\u8ba1\u548c\u7528\u6237\u4f53\u9a8c\u7684\u9700\u6c42\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd App \u4e2d\u7684 style \u6807\u7b7e\uff0c\u5305\u62ec\u5b83\u7684\u539f\u7406\u3001\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u8fdb\u884c\u81ea\u5b9a\u4e49\u6837\u5f0f\u3002<\/p>\n

## \u6837\u5f0f\u539f\u7406<\/p>\n

\u4e00\u4e2a App \u901a\u5e38\u7531\u591a\u79cd\u7528\u6237\u754c\u9762 (UI) \u7ec4\u6210\uff0c\u5305\u62ec\u6309\u94ae\u3001\u6587\u672c\u6846\u3001\u56fe\u7247\u7b49\uff0c\u8fd9\u4e9b UI \u5143\u7d20\u9700\u8981\u4e00\u5b9a\u7684\u7f8e\u89c2\u6027\u6765\u63d0\u9ad8\u6574\u4f53\u7684\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n

<\/figure>\n<\/p>\n

\u800c style \u6807\u7b7e\u5728\u8bb8\u591a\u5f00\u53d1\u5e73\u53f0\u4e2d\u90fd\u6709\u7740\u76f8\u4f3c\u7684\u529f\u80fd\uff0c\u5b83\u7528\u4e8e\u62bd\u79bb UI \u5143\u7d20\u7684\u6837\u5f0f\u5c5e\u6027\uff0c\u65b9\u4fbf\u5728\u5404\u4e2a UI \u7ec4\u4ef6\u4e2d\u8fdb\u884c\u590d\u7528\u3002<\/p>\n

\u5728 App \u5f00\u53d1\u4e2d\uff0c\u6837\u5f0f\u901a\u5e38\u5305\u62ec\u989c\u8272\u3001\u5b57\u4f53\u3001\u95f4\u8ddd\u3001\u5bf9\u9f50\u7b49\u65b9\u9762\u3002\u901a\u8fc7\u4e3a\u8fd9\u4e9b\u6837\u5f0f\u5b9a\u4e49\u4e00\u7ec4\u89c4\u5219\uff0c\u53ef\u4ee5\u4fdd\u8bc1\u6574\u4e2a App \u7684\u89c6\u89c9\u98ce\u683c\u4fdd\u6301\u4e00\u81f4\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n

## style \u6807\u7b7e\u4f7f\u7528\u65b9\u6cd5<\/p>\n

\u4e0d\u540c\u7684\u5f00\u53d1\u5e73\u53f0\u548c\u8bed\u8a00\u53ef\u80fd\u6709\u4e0d\u540c\u7684 style \u6807\u7b7e\u5199\u6cd5\u3002\u4ee5\u4e0b\u6211\u4eec\u5206\u522b\u4ee5 Android \u548c iOS \u4e3a\u4f8b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\u5982\u4f55\u5728\u5b83\u4eec\u7684\u5f00\u53d1\u73af\u5883\u4e2d\u4f7f\u7528 style \u6807\u7b7e\u3002<\/p>\n

### Android<\/p>\n

\u5728 Android \u5f00\u53d1\u4e2d\uff0cstyle \u6807\u7b7e\u4e3b\u8981\u88ab\u7528\u4e8e\u5b9a\u4e49\u4e00\u4e2a\u4e3b\u9898\uff08Theme\uff09\uff0c\u4ece\u800c\u521b\u5efa\u4e00\u7ec4\u989c\u8272\u3001\u5b57\u4f53\u548c\u63a7\u4ef6\u6837\u5f0f\u7684\u89c4\u5219\u3002\u8fd9\u4e9b\u4e3b\u9898\u53ef\u4ee5\u5728\u9879\u76ee\u7684 styles.xml \u6587\u4ef6\u4e2d\u5b9a\u4e49\u3002<\/p>\n

\u4e3a\u4e86\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6837\u5f0f\uff0c\u4f60\u9700\u8981\u5728 styles.xml \u6587\u4ef6\u4e2d\u7f16\u5199\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n

“`xml<\/p>\n

\n

<\/p>\n

@color\/colorPrimary<\/p>\n

APP\u5f00\u53d1<\/a> @color\/colorPrimaryDark<\/p>\n

@color\/colorAccent<\/p>\n<\/p>\n

“`<\/p>\n

\u6b64\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a “AppTheme” \u7684\u4e3b\u9898\uff0c\u5e76\u6307\u5b9a\u4e86\u4e00\u4e9b\u989c\u8272\u8d44\u6e90\u4ee5\u53ca\u7ee7\u627f\u81ea `Theme.AppCompat.Light.DarkActionBar` \u7684\u6837\u5f0f\u3002<\/p>\n

\u8981\u4f7f\u7528\u8fd9\u4e2a\u4e3b\u9898\uff0c\u4f60\u9700\u8981\u5728 AndroidManifest.xml \u6587\u4ef6\u4e2d\u5c06\u5176\u5e94\u7528\u5230\u6574\u4e2a App \u6216\u8005\u7279\u5b9a\u7684 Activity\uff1a<\/p>\n

“`xml<\/p>\n<\/p>\n

android:theme=”@style\/AppTheme”><\/p>\n

…<\/p>\n

“`<\/p>\n

### iOS<\/p>\n

\u5728 iOS \u5f00\u53d1\u4e2d\uff0c\u4f60\u901a\u5e38\u4f1a\u4f7f\u7528 Interface Builder \u548c Storyboard \u6587\u4ef6\u6765\u8bbe\u8ba1 UI\u3002\u867d\u7136 iOS \u5e73\u53f0\u6ca1\u6709\u7c7b\u4f3c\u4e8e style \u6807\u7b7e\u7684\u6982\u5ff5\uff0c\u4f46\u4f60\u53ef\u4ee5\u4f7f\u7528\u5916\u90e8\u5e93\uff08\u5982 SwiftGen \u6216 R.swift\uff09\u6765\u751f\u6210\u989c\u8272\u3001\u5b57\u4f53\u7b49\u8d44\u6e90\u7684\u4ee3\u7801\uff0c\u5e76\u5728\u5e94\u7528\u4e2d\u4f7f\u7528\u5b83\u4eec\u3002<\/p>\n

APP<\/a><\/p>\n

\u53e6\u5916\uff0c\u901a\u8fc7\u6269\u5c55\uff08extension\uff09\u548c\u81ea\u5b9a\u4e49\u7c7b\uff08subclassing\uff09\u7684\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u5728\u591a\u4e2a UI \u7ec4\u4ef6\u4e2d\u5f15\u7528\u7edf\u4e00\u7684\u6837\u5f0f\u3002<\/p>\n

\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4e3a UILabel \u521b\u5efa\u4e00\u4e2a\u6269\u5c55\u6765\u5b9a\u4e49\u7edf\u4e00\u7684\u6587\u672c\u6837\u5f0f\uff1a<\/p>\n

“`swift<\/p>\n

extension UILabel {<\/p>\n

func applyPrimaryStyle() {<\/p>\n

self.font = UIFont.systemFont(ofSize: 16)<\/p>\n

self.textColor = UIColor.blue<\/p>\n

}<\/p>\n

}<\/p>\n

“`<\/p>\n

\u901a\u8fc7\u8c03\u7528 `applyPrimaryStyle` \u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u6837\u5f0f\u5e94\u7528\u5230\u5404\u4e2a UILabel \u5b9e\u4f8b\uff1a<\/p>\n

“`swift<\/p>\n

label1.applyPrimaryStyle()<\/p>\n

label2.applyPrimaryStyle()<\/p>\n

“`<\/p>\n

## \u81ea\u5b9a\u4e49\u6837\u5f0f<\/p>\n

\u968f\u7740 App \u5f00\u53d1\u7684\u8fdb\u884c\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u521b\u5efa\u5404\u79cd\u4e0d\u540c\u7684\u6837\u5f0f\u4ee5\u9002\u5e94\u590d\u6742\u7684 UI \u8bbe\u8ba1\u3002\u8fd9\u65f6\u53ef\u4ee5\u5728 style \u6807\u7b7e\u7684\u57fa\u7840\u4e0a\u6dfb\u52a0\u66f4\u591a\u7684\u6837\u5f0f\u5c5e\u6027\uff0c\u4ee5\u5b9e\u73b0\u7279\u5b9a\u7684\u9700\u6c42\u3002<\/p>\n

\u4f8b\u5982\uff0c\u5728 Android \u4e2d\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6309\u94ae\u6837\u5f0f\uff1a<\/p>\n

“`xml<\/p>\n

\n

18sp<\/p>\n

@color\/white<\/p>\n

@color\/blue<\/p>\n<\/p>\n

“`<\/p>\n

\u5c06\u8fd9\u4e2a\u6837\u5f0f\u5e94\u7528\u5230 Button \u63a7\u4ef6\uff1a<\/p>\n

“`xml<\/p>\n<\/p>\n

style=”@style\/CustomButton”<\/p>\n

… \/><\/p>\n

“`<\/p>\n

\u7c7b\u4f3c\u5730\uff0c\u5728 iOS \u5f00\u53d1\u4e2d\uff0c\u4f60\u4e5f\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u81ea\u5b9a\u4e49\u7c7b\u6216\u8005\u6269\u5c55\u6765\u5b9e\u73b0\u66f4\u591a\u6837\u5f0f\u7684\u5b9a\u5236\u3002<\/p>\n

## \u603b\u7ed3<\/p>\n

style \u6807\u7b7e\u5728 App \u5f00\u53d1\u4e2d\u5177\u6709\u5f88\u5f3a\u7684\u5b9e\u7528\u6027\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8f7b\u677e\u5730\u5b9e\u73b0\u5404\u79cd\u89c6\u89c9\u6837\u5f0f\u548c\u98ce\u683c\uff0c\u5e76\u4fdd\u8bc1 App \u7684\u6574\u4f53\u7f8e\u89c2\u548c\u7528\u6237\u4f53\u9a8c\u3002\u901a\u8fc7\u638c\u63e1 style \u6807\u7b7e\u7684\u4f7f\u7528\u65b9\u6cd5\u548c\u81ea\u5b9a\u4e49\u6280\u5de7\uff0c\u76f8\u4fe1\u4f60\u53ef\u4ee5\u521b\u5efa\u51fa\u66f4\u52a0\u4f18\u79c0\u7684 App\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728 App \u5f00\u53d1\u4e2d\uff0c\u6837\u5f0f\u662f\u4e00\u79cd\u91cd\u8981\u7684\u5143\u7d20\uff0c\u5b83\u8ba9\u89c6\u89c9\u8868\u73b0\u5c42\u66f4\u52a0\u7b26\u5408\u8bbe\u8ba1\u548c\u7528\u6237\u4f53\u9a8c\u7684\u9700\u6c42\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd App \u4e2d\u7684 style \u6807\u7b7e\uff0c\u5305\u62ec\u5b83\u7684\u539f\u7406\u3001\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u8fdb\u884c\u81ea\u5b9a<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[2383,2382,1237,754,926],"topic":[],"class_list":["post-1036","post","type-post","status-publish","format-standard","hentry","category-appkf","tag-discuz","tag-2382","tag-1237","tag-754","tag-926"],"_links":{"self":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/1036","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/comments?post=1036"}],"version-history":[{"count":0,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/1036\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/media?parent=1036"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/categories?post=1036"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/tags?post=1036"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/topic?post=1036"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}