{"id":79224,"date":"2026-05-08T17:23:10","date_gmt":"2026-05-08T09:23:10","guid":{"rendered":"https:\/\/www.yimenapp.com\/kb-yimen\/?p=79224"},"modified":"2026-05-08T17:23:10","modified_gmt":"2026-05-08T09:23:10","slug":"%e8%87%aa%e5%88%b6%e5%ae%89%e5%8d%93app%e6%a1%8c%e9%9d%a2%e5%b0%8f%e5%b7%a5%e5%85%b7%e6%8e%a8%e8%8d%90%e5%93%aa%e4%ba%9b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.yimenapp.com\/kb-yimen\/79224\/","title":{"rendered":"\u81ea\u5236\u5b89\u5353app\u684c\u9762\u5c0f\u5de5\u5177\u63a8\u8350\u54ea\u4e9b\uff1f"},"content":{"rendered":"<p>\u5728\u5b89\u5353\u7cfb\u7edf\u4e2d\uff0c\u684c\u9762<span class=\"wpcom_tag_link\"><a href=\"https:\/\/www.yimenapp.com\/kb-yimen\/tag\/%e5%b0%8f%e5%b7%a5%e5%85%b7\/\" title=\"\u5c0f\u5de5\u5177\" target=\"_blank\">\u5c0f\u5de5\u5177<\/a><\/span>\u662f\u4e00\u4e2a\u975e\u5e38\u5b9e\u7528\u7684\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u5c06\u6211\u4eec\u7ecf\u5e38\u4f7f\u7528\u7684\u5e94\u7528\u7a0b\u5e8f\u3001<a href=\"https:\/\/www.yimenapp.cn\/fengzhuang-2127.html\">\u81ea\u5df1\u80fd\u505aapp<\/a>\u4fe1\u606f\u3001\u5929\u6c14\u7b49\u5185\u5bb9\u76f4\u63a5\u5c55\u793a\u5728\u684c\u9762\u4e0a\uff0c\u65b9\u4fbf\u7528\u6237\u5feb\u901f\u8bbf\u95ee\u3002\u5982\u679c\u4f60\u60f3\u81ea\u5df1\u5236\u4f5c\u4e00\u4e2a\u5b89\u5353<span class=\"wpcom_keyword_link\"><a href=\"https:\/\/www.yimenapp.com\" target=\"_blank\" title=\"\u5f00\u53d1app\u4ece\u4e00\u95e8\u5f00\u59cb\">app<\/a><\/span>\u684c\u9762\u5c0f\u5de5\u5177\uff0c\u672c\u6587\u5c06\u4e3a\u4f60\u4ecb\u7ecd\u5236\u4f5c\u7684\u539f\u7406\u548c\u8be6\u7ec6\u6b65\u9aa4\u3002<\/p>\n<p>\u4e00\u3001\u5236\u4f5c\u539f\u7406<a href=\"https:\/\/www.yimenapp.cn\/fengzhuang-1026.html\">\u5236\u4f5capp<\/a><\/p>\n<p>\u5b89\u5353\u684c\u9762\u5c0f\u5de5\u5177\u7684\u5236\u4f5c\uff0c\u4e3b\u8981\u662f\u901a\u8fc7<span class=\"wpcom_tag_link\"><a href=\"https:\/\/www.yimenapp.com\/kb-yimen\/tag\/%e8%87%aa%e5%ae%9a%e4%b9%89\/\" title=\"\u81ea\u5b9a\u4e49\" target=\"_blank\">\u81ea\u5b9a\u4e49<\/a><\/span>View\u548cAppWidgetProvider\u6765\u5b9e\u73b0\u7684\u3002<\/p>\n<p>\u81ea\u5b9a\u4e49View\u662f\u6307\u6211\u4eec\u81ea\u5df1\u7f16\u5199\u7684\u7ee7\u627f\u81eaView\u7684\u7c7b\uff0c\u5b83\u53ef\u4ee5\u901a\u8fc7\u7ed8\u5236\u6765\u5c55\u793a\u6211\u4eec\u9700\u8981\u7684\u5185\u5bb9\u3002\u800cAppWidgetProvider\u5219\u662f\u4e00\u4e2a\u5e7f\u64ad\u63a5\u6536\u5668\uff0c\u5b83\u4f1a\u5728\u684c\u9762\u5c0f\u5de5\u5177\u88ab\u521b\u5efa\u6216\u66f4\u65b0\u7684\u65f6\u5019\u88ab\u89e6\u53d1\uff0c<span class=\"wpcom_tag_link\"><a href=\"https:\/\/www.yimenapp.com\/kb-yimen\/tag\/%e6%88%91%e4%bb%ac%e5%8f%af%e4%bb%a5\/\" title=\"\u6211\u4eec\u53ef\u4ee5\" target=\"_blank\">\u6211\u4eec\u53ef\u4ee5<\/a><\/span>\u5728\u5b83\u7684onUpdate()\u65b9\u6cd5\u4e2d\u66f4\u65b0\u81ea\u5b9a\u4e49View\u7684\u5185\u5bb9\u5e76\u5c06\u7ed3\u679c\u5c55\u793a\u5728\u684c\u9762\u4e0a\u3002<\/p>\n<p>\u4e8c\u3001\u5236\u4f5c\u6b65\u9aa4<\/p>\n<p>1.\u521b\u5efa\u9879\u76ee<\/p>\n<p>\u6253\u5f00Android Studio\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u9879\u76ee\u3002\u9009\u62e9Empty Activity\u5e76\u8bbe\u7f6e\u540d\u79f0\uff0c\u70b9\u51fbFinish\u521b\u5efa\u9879\u76ee\u3002<\/p>\n<p>2.\u521b\u5efa\u81ea\u5b9a\u4e49View<\/p>\n<p>\u53f3\u952e\u70b9\u51fbapp\u76ee\u5f55\uff0c\u9009\u62e9New -&gt; Java Class\uff0c\u8bbe\u7f6e\u540d\u79f0\u4e3aMyView\u5e76\u7ee7\u627f\u81eaView\u3002\u5728MyView\u4e2d\u91cd\u5199onDraw()\u65b9\u6cd5\uff0c\u7ed8\u5236\u6211\u4eec\u9700\u8981\u7684\u5185\u5bb9\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed8\u5236\u4e00\u4e2a\u5706\u5f62\u548c\u4e00\u6bb5\u6587\u5b57\uff1a<\/p>\n<p>&#8220;`java<\/p>\n<p>public class MyView extends View {<\/p>\n<p>    private Paint mPaint;<\/p>\n<p>    public MyView(Context context) {<\/p>\n<p>        super(context);<\/p>\n<p>        mPaint = new Paint();<\/p>\n<p>        mPaint.setColor(Color.RED);<\/p>\n<p>        mPaint.setTextSize(50);<\/p>\n<p>    }<\/p>\n<p>    @Override<\/p>\n<p>    protected void onDraw(Canvas canvas) {<\/p>\n<p>        super.onDraw(canvas);<\/p>\n<p>        canvas.drawCircle(getWidth() \/ 2, getHeight() \/ 2, 100, mPaint);<\/p>\n<p>        canvas.drawText(&#8220;Hello World&#8221;, getWidth() \/ 2 &#8211; 100, getHeight() \/ 2 + 50, mPaint);<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>3.\u521b\u5efaAppWidgetProvider<\/p>\n<p>\u53f3\u952e\u70b9\u51fbapp\u76ee\u5f55\uff0c\u9009\u62e9New -&gt; Other -&gt; Widget -&gt; App Widget Provider\uff0c\u8bbe\u7f6e\u540d\u79f0\u4e3aMyWidgetProvider\u3002\u5728MyWidgetProvider\u4e2d\u91cd\u5199onUpdate()\u65b9\u6cd5\uff0c\u66f4\u65b0\u81ea\u5b9a\u4e49View\u7684\u5185\u5bb9\u5e76\u5c06\u7ed3\u679c\u5c55\u793a\u5728\u684c\u9762\u4e0a\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5728onUpdate()\u65b9\u6cd5\u4e2d\u521b\u5efa\u4e00\u4e2aRemoteViews\u5bf9\u8c61\uff0c\u8bbe\u7f6e\u5e03\u5c40\u548c\u81ea\u5b9a\u4e49View\uff0c\u5e76\u5c06\u5176\u66f4\u65b0\u5230\u684c\u9762\u4e0a\uff1a<\/p>\n<p>&#8220;`java<\/p>\n<p>public class MyWidgetProvider extends AppWidgetProvider {<\/p>\n<p>    @Override<\/p>\n<p>    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {<\/p>\n<p>        for (int appWidgetId : appWidgetIds) {<\/p>\n<p>            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);<\/p>\n<p>            views.setImageViewResource(R.id.image_view, R.drawable.ic_launcher);<\/p>\n<p>            views.setTextViewText(R.id.text_view, &#8220;Hello Widget&#8221;);<\/p>\n<p>            views.setImageViewBitmap(R.id.my_view, getBitmap(context));<\/p>\n<p>            appWidgetManager.updateAppWidget(appWidgetId, views);<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>    private Bitmap getBitmap(Context context) {<\/p>\n<p>        MyView myView = new MyView(context);<\/p>\n<p>        myView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),<\/p>\n<p>                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));<\/p>\n<p>        myView.layout(0, 0, myView.getMeasuredWidth(), myView.getMeasuredHeight());<\/p>\n<p>        Bitmap bitmap = Bitmap.createBitmap(myView.getMeasuredWidth(), myView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);<\/p>\n<p>        Canvas canvas = new Canvas(bitmap);<\/p>\n<p>        myView.draw(canvas);<\/p>\n<p>        return bitmap;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aRemoteViews\u5bf9\u8c61\uff0c\u8bbe\u7f6e\u4e86\u5e03\u5c40\u548c\u81ea\u5b9a\u4e49View\uff0c\u5e76\u5c06\u5176\u66f4\u65b0\u5230\u684c\u9762\u4e0a\u3002\u6211\u4eec\u8fd8\u521b\u5efa\u4e86\u4e00\u4e2agetBitmap()\u65b9\u6cd5\uff0c\u7528\u6765\u5c06\u81ea\u5b9a\u4e49View\u8f6c\u6362\u6210Bitmap\u5bf9\u8c61\u3002<\/p>\n<p>4.\u521b\u5efa\u5e03\u5c40\u6587\u4ef6<\/p>\n<p>\u5728res\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3awidget_layout.xml\u7684\u5e03\u5c40\u6587\u4ef6\uff0c\u8bbe\u7f6e\u4e00\u4e2aImageView\u548c\u4e00\u4e2aTextView\u7528\u6765\u5c55\u793a\u4e00\u4e9b\u57fa\u672c\u4fe1\u606f\uff0c\u518d\u52a0\u4e0a\u6211\u4eec\u81ea\u5df1\u7f16\u5199\u7684MyView\u7528\u6765\u5c55\u793a\u81ea\u5b9a\u4e49\u7684\u5185\u5bb9\u3002<\/p>\n<p>&#8220;`xml<\/p>\n<\/p>\n<p>    android:layout_width=&#8221;match_parent&#8221;<\/p>\n<p>    android:layout_height=&#8221;match_parent&#8221;&gt;<\/p>\n<\/p>\n<p>        android:id=&#8221;@+id\/image_view&#8221;<\/p>\n<p>        android:layout_width=&#8221;wrap_content&#8221;<\/p>\n<p>        android:layout_height=&#8221;wrap_content&#8221;<\/p>\n<p>        android:src=&#8221;@drawable\/ic_launcher&#8221;<\/p>\n<p>        android:layout_centerHorizontal=&#8221;true&#8221;<\/p>\n<p>        android:layout_marginTop=&#8221;20dp&#8221;\/&gt;<\/p>\n<\/p>\n<p>        android:id=&#8221;@+id\/text_view&#8221;<\/p>\n<p>        android:layout_width=&#8221;wrap_content&#8221;<\/p>\n<p>        android:layout_height=&#8221;wrap_content&#8221;<\/p>\n<p>        android:text=&#8221;Hello Widget&#8221;<\/p>\n<p>        android:layout_below=&#8221;@id\/image_view&#8221;<\/p>\n<p>        android:layout_centerHorizontal=&#8221;true&#8221;<\/p>\n<p>        android:layout_marginTop=&#8221;20dp&#8221;\/&gt;<\/p>\n<\/p>\n<p>        android:id=&#8221;@+id\/my_view&#8221;<\/p>\n<p>        android:layout_width=&#8221;wrap_content&#8221;<\/p>\n<p>        android:layout_height=&#8221;wrap_content&#8221;<\/p>\n<p>        android:layout_below=&#8221;@id\/text_view&#8221;<\/p>\n<p>        android:layout_centerHorizontal=&#8221;true&#8221;\/&gt;<\/p>\n<p><p>&#8220;`<\/p>\n<p>5.\u914d\u7f6eAndroidManifest.xml<\/p>\n<p>\u5728AndroidManifest.xml\u6587\u4ef6\u4e2d\u6dfb\u52a0\u5982\u4e0b\u4ee3\u7801\uff0c\u58f0\u660eMyWidgetProvider\u548cMyView\u3002<\/p>\n<p>&#8220;`xml<\/p>\n<p><p><\/p>\n<\/p>\n<p>&#8220;`<\/p>\n<p>\u5728res\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3amy_widget_provider.xml\u7684\u6587\u4ef6\uff0c\u8bbe\u7f6eAppWidgetProviderInfo\u7684\u5c5e\u6027\u3002<\/p>\n<p>&#8220;`xml<\/p>\n<\/p>\n<p>    android:minWidth=&#8221;20<\/p>\n<p><figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i.yimenyun.net\/2023-03\/24\/13\/16341591621.jpg\" \/><\/figure><\/p>\n<p>0dp&#8221;<\/p>\n<p>    android:minHeight=&#8221;200dp&#8221;<\/p>\n<p>    android:updatePeriodMillis=&#8221;86400000&#8243;<\/p>\n<p>    android:previewImage=&#8221;@drawable\/ic_launcher&#8221;<\/p>\n<p>    android:initialLayout=&#8221;@layout\/widget_layout&#8221;<\/p>\n<p>    android:resizeMode=&#8221;horizontal|vertical&#8221;<\/p>\n<p>    android:widgetCategory=&#8221;home_screen&#8221; \/&gt;<\/p>\n<p>&#8220;`<\/p>\n<p>6.\u8fd0\u884c\u7a0b\u5e8f<\/p>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd0\u884c\u7a0b\u5e8f\uff0c\u5728\u5b89\u5353\u7cfb\u7edf\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u684c\u9762\u5c0f\u5de5\u5177\uff0c\u5c31\u53ef\u4ee5\u770b\u5230\u6211\u4eec\u81ea\u5df1\u7f16\u5199\u7684\u5c0f\u5de5\u5177\u4e86\u3002<\/p>\n<p>\u4e09\u3001\u603b\u7ed3<\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86\u5236\u4f5c\u5b89\u5353app\u684c\u9762\u5c0f\u5de5\u5177\u7684\u539f\u7406\u548c\u8be6\u7ec6\u6b65\u9aa4\u3002\u901a\u8fc7\u81ea\u5b9a\u4e49View\u548cAppWidgetProvider\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u521b\u5efa\u81ea\u5df1\u7684\u684c\u9762\u5c0f\u5de5\u5177\uff0c\u63d0\u9ad8\u6211\u4eec\u7684\u751f\u4ea7\u529b\u548c\u4f7f\u7528\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5b89\u5353\u7cfb\u7edf\u4e2d\uff0c\u684c\u9762\u5c0f\u5de5\u5177\u662f\u4e00\u4e2a\u975e\u5e38\u5b9e\u7528\u7684\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u5c06\u6211\u4eec\u7ecf\u5e38\u4f7f\u7528\u7684\u5e94\u7528\u7a0b\u5e8f\u3001\u4fe1\u606f\u3001\u5929\u6c14\u7b49\u5185\u5bb9\u76f4\u63a5\u5c55\u793a\u5728\u684c\u9762\u4e0a\uff0c\u65b9\u4fbf\u7528\u6237\u5feb\u901f\u8bbf\u95ee\u3002\u5982\u679c\u4f60\u60f3\u81ea\u5df1\u5236\u4f5c\u4e00\u4e2a\u5b89\u5353app\u684c\u9762\u5c0f\u5de5\u5177\uff0c\u672c\u6587\u5c06\u4e3a\u4f60\u4ecb\u7ecd\u5236\u4f5c\u7684\u539f\u7406\u548c<\/p>\n","protected":false},"author":20,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8057],"tags":[16910,20588,8936,1873,9135],"special":[],"attr":[],"class_list":["post-79224","post","type-post","status-publish","format-standard","hentry","category-appkaifa","tag-webappvue","tag-20588","tag-8936","tag-1873","tag-9135","entry"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/posts\/79224","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/comments?post=79224"}],"version-history":[{"count":1,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/posts\/79224\/revisions"}],"predecessor-version":[{"id":79258,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/posts\/79224\/revisions\/79258"}],"wp:attachment":[{"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/media?parent=79224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/categories?post=79224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/tags?post=79224"},{"taxonomy":"special","embeddable":true,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/special?post=79224"},{"taxonomy":"attr","embeddable":true,"href":"https:\/\/www.yimenapp.com\/kb-yimen\/wp-json\/wp\/v2\/attr?post=79224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}