{"id":170,"date":"2023-05-18T12:05:30","date_gmt":"2023-05-18T04:05:30","guid":{"rendered":"https:\/\/app.60day.cn\/appday\/?p=170"},"modified":"2023-05-18T12:05:30","modified_gmt":"2023-05-18T04:05:30","slug":"android-popupwindow%e5%b0%81%e8%a3%85%e6%9c%89%e4%bb%80%e4%b9%88%e4%bd%9c%e7%94%a8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/app.60day.cn\/appday\/kaifaapp\/170\/","title":{"rendered":"android popupwindow\u5c01\u88c5\u6709\u4ec0\u4e48\u4f5c\u7528\uff1f"},"content":{"rendered":"<p>PopupWindow\u662fAndroid\u4e2d\u5e38\u7528\u7684\u4e00\u4e2a\u5f39\u51fa\u6846\u63a7\u4ef6\uff0c\u53ef\u4ee5\u7528\u4e8e\u663e\u793a\u4e00\u4e9b\u81ea\u5b9a\u4e49\u7684\u89c6\u56fe\uff0c\u6bd4\u5982\u83dc\u5355\u3001\u63d0\u793a\u6846\u3001\u9009\u62e9\u5668\u7b49\u7b49\u3002PopupWindow\u7684\u4f7f\u7528\u975e\u5e38\u7075\u6d3b\uff0c\u4f46\u662f\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u4e00\u4e9b\u5c01\u88c5\uff0c\u4ee5\u65b9\u4fbf\u4ee3\u7801\u7684\u590d\u7528\u548c\u7ef4\u62a4\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5bf9PopupWindow\u8fdb\u884c\u5c01\u88c5\u3002<\/p>\n<p>\u4e00\u3001PopupWindow\u7684\u57fa\u672c\u4f7f\u7528<\/p>\n<p>\u5728\u4f7f\u7528Popup<a href=\"https:\/\/www.yimenapp.net\/dabao-1129.html\">WEb Clip app<\/a>Window\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u521b\u5efa\u4e00\u4e2a\u5e03\u5c40\u6587\u4ef6\uff0c\u7528\u4e8e\u663e\u793a\u6211\u4eec\u81ea\u5b9a\u4e49\u7684\u89c6\u56fe\u3002\u6bd4\u5982\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u540d\u4e3apopup_window.xml\u7684\u5e03\u5c40\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2aTextView\u548c\u4e00\u4e2aButton\u3002<\/p>\n<p>&#8220;`<\/p>\n<\/p>\n<p>    android:layout_width=&#8221;wrap_content&#8221;<\/p>\n<p>    andr<\/p>\n<p><figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i.cdn.yimenapp.com\/2023-03\/31\/15\/14050311244.jpg\" \/><\/figure>\n<\/p>\n<p>oid:layout_height=&#8221;wrap_content&#8221;<\/p>\n<p>    android:orientation=&#8221;vertical&#8221;<\/p>\n<p>    android:padding=&#8221;10dp&#8221;&gt;<\/p>\n<\/p>\n<p>        android:id=&#8221;@+id\/tv_content&#8221;<\/p>\n<p>        android:layout_width=&#8221;match_parent&#8221;<\/p>\n<p>        android:layout_height=&#8221;wrap_content&#8221;<\/p>\n<p>        android:text=&#8221;\u8fd9\u662f\u4e00\u4e2aPopupWindow&#8221;<\/p>\n<p>        android:textSize=&#8221;16sp&#8221; \/&gt;<\/p>\n<\/p>\n<p>        android:id=&#8221;@+id\/btn_close&#8221;<\/p>\n<p>        android:layout_width=&#8221;match_parent&#8221;<\/p>\n<p>        android:layout_height=&#8221;wrap_content&#8221;<\/p>\n<p>        android:text=&#8221;\u5173\u95ed&#8221; \/&gt;<\/p>\n<p><p>&#8220;`<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5728Activity\u4e2d\u521b\u5efa\u4e00\u4e2aPopupWindow\u5bf9\u8c61\uff0c\u5e76\u5c06\u5176\u4e0e\u6211\u4eec\u521a\u624d\u521b\u5efa\u7684\u5e03\u5c40\u6587\u4ef6\u76f8\u5173\u8054\u3002<\/p>\n<p>&#8220;`<\/p>\n<p>PopupWindow popupWindow = new PopupWindow(this);<\/p>\n<p>View contentView = LayoutInflater.from(this).inflate(R.layout.popup_window, null);<\/p>\n<p>popupWindow.setContentView(contentView);<\/p>\n<p>&#8220;`<\/p>\n<p>\u7136\u540e\u6211\u4eec\u53ef\u4ee5\u5bf9PopupWindow\u8fdb\u884c\u4e00\u4e9b\u57fa\u672c\u7684\u914d\u7f6e\uff0c\u6bd4\u5982\u8bbe\u7f6e\u5bbd\u9ad8\u3001\u80cc\u666f\u3001\u52a8\u753b\u7b49\u7b49\u3002<\/p>\n<p>&#8220;`<\/p>\n<p>popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);<\/p>\n<p>popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);<\/p>\n<p>popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));<\/p>\n<p>popupWindow.setAnimationStyle(R.style.PopupAnimation);<\/p>\n<p>&#8220;`<\/p>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u9700\u8981\u5c06PopupWindow\u663e\u793a\u51fa\u6765\u3002<\/p>\n<p>&#8220;`<\/p>\n<p>popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);<\/p>\n<p>&#8220;`<\/p>\n<p>\u5176\u4e2d\uff0cview\u8868\u793aPopupWindow\u7684\u951a\u70b9\uff0cGravity\u8868\u793aPopupWindow\u663e\u793a\u7684\u4f4d\u7f6e\uff0c0\u548c0\u8868\u793a\u504f\u79fb\u91cf\u3002<\/p>\n<p>\u4e8c\u3001PopupWindow\u7684\u5c01\u88c5<\/p>\n<p>\u4e0a\u9762\u4ecb\u7ecd\u4e86PopupWindow\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\uff0c\u4f46\u662f\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u5c01\u88c5\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u4ecb\u7ecd\u4e00\u79cd\u5e38\u89c1\u7684PopupWindow\u5c01\u88c5\u65b9\u5f0f\u3002<\/p>\n<p>1. \u521b\u5efaPopupWindowHelper\u7c7b<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2aPopupWindowHelper\u7c7b\uff0c\u7528\u4e8e\u5bf9PopupWindow\u8fdb\u884c\u5c01\u88c5\u3002\u8be5\u7c7b\u4e2d\u5305\u542b\u4e86PopupWindow\u7684\u57fa\u672c\u64cd\u4f5c\uff0c\u6bd4\u5982\u521b\u5efa\u3001\u663e\u793a\u3001\u5173\u95ed\u7b49\u7b49\u3002<\/p>\n<p>&#8220;`<\/p>\n<p>public class PopupWindowHelper {<\/p>\n<p>    private Pop<a href=\"https:\/\/www.yimenapp.net\/dabao-2850.html\">txt\u751f\u6210apk<\/a>upWindow mPopupWindow;<\/p>\n<p>    public void createPopupWindow(Context context, int layoutResId) {<\/p>\n<p>        View contentView = LayoutInflater.from(context).inflate(layoutResId, null);<\/p>\n<p>        mPopupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);<\/p>\n<p>        mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));<\/p>\n<p>        mPopupWindow.setAnimationStyle(R.style.PopupAnimation);<\/p>\n<p>    }<\/p>\n<p>    public void showPopupWindow(View anchorView) {<\/p>\n<p>        if (mPopupWindow != null &amp;&amp; !mPopupWindow.isShowing()) {<\/p>\n<p>            mPopupWindow.showAtLocation(anchorView, Gravity.CENTER, 0, 0);<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>    public void dismissPopupWindow() {<\/p>\n<p>        if (mPopupWindow != null &amp;&amp; mPopupWindow.isShowing()) {<\/p>\n<p>            mPopupWindow.dismiss();<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>2. \u5728Activity\u4e2d\u4f7f\u7528PopupWindowHelper<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Activity\u4e2d\u4f7f\u7528PopupWindowHelper\u7c7b\uff0c\u5bf9PopupWindow\u8fdb\u884c\u64cd\u4f5c\u3002\u6bd4\u5982\u6211\u4eec\u53ef\u4ee5\u5728Activity\u4e2d\u521b\u5efa\u4e00\u4e2aPopupWindowHelper\u5bf9\u8c61\uff0c\u7136\u540e\u5728\u9700\u8981\u663e\u793aPopupWindow\u7684\u65f6\u5019\u8c03\u7528showPopupWindow\u65b9\u6cd5\u3002<\/p>\n<p>&#8220;`<\/p>\n<p>public class MainActivity extends AppCompatActivity {<\/p>\n<p>    private PopupWindowHelper mPopupWindowHelper;<\/p>\n<p>    @Override<\/p>\n<p>    protected void onCreate(Bundle savedInstanceState) {<\/p>\n<p>        super.onCreate(savedInstanceState);<\/p>\n<p>        setContentView(R.layout.activity_main);<\/p>\n<p>        mPopupWindowHelper = new PopupWindowHelper();<\/p>\n<p>        mPopupWindowHelper.createPopupWindow(this, R.layout.popup_window);<\/p>\n<p>        Button button = findViewById(R.id.btn_show_popup_window);<\/p>\n<p>        button.setOnClickListener(new View.OnClickListener() {<\/p>\n<p>            @Override<\/p>\n<p>            public void onClick(View v) {<\/p>\n<p>                mPopupWindowHelper.showPopupWindow(v);<\/p>\n<p>            }<\/p>\n<p>        });<\/p>\n<p>        TextView textView = mPopupWindowHelper.getContentView().findViewById(R.id.tv_content);<\/p>\n<p>        textView.setText(&#8220;\u8fd9\u662f\u4e00\u4e2a\u5c01\u88c5\u540e\u7684PopupWindow&#8221;);<\/p>\n<p>    }<\/p>\n<p>    @Override<\/p>\n<p>    protected void onDestroy() {<\/p>\n<p>        super.onDestroy();<\/p>\n<p>        mPopupWindowHelper.dismissPopupWindow();<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>\u5176\u4e2d\uff0c\u6211\u4eec\u5728Activity\u7684onCreate\u65b9\u6cd5\u4e2d\u521b\u5efa\u4e86\u4e00\u4e2aPopupWindowHelper\u5bf9\u8c61\uff0c\u5e76\u5c06\u5176\u4e0e\u6211\u4eec\u521a\u624d\u521b\u5efa\u7684\u5e03\u5c40\u6587\u4ef6\u76f8\u5173\u8054\u3002\u7136\u540e\u6211\u4eec\u53ef\u4ee5\u5728\u9700\u8981\u663e\u793aPopupWindow\u7684\u65f6\u5019\u8c03\u7528showPopupWindow\u65b9\u6cd5\uff0c\u5c06PopupWindow\u663e\u793a\u51fa\u6765\u3002\u6700\u540e\uff0c\u5728Activity\u7684onDestroy\u65b9\u6cd5\u4e2d\u8c03\u7528dismissPopupWindow\u65b9\u6cd5\uff0c\u5c06PopupWindow\u5173\u95ed\u3002<\/p>\n<p>\u4e09\u3001\u603b\u7ed3<\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86PopupWindow\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\uff0c\u5e76\u5bf9\u5176\u8fdb\u884c\u4e86\u5c01\u88c5\u3002\u901a\u8fc7\u5c01\u88c5\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06PopupWindow\u7684\u57fa\u672c\u64cd\u4f5c\u5c01\u88c5\u5230\u4e00\u4e2a\u7c7b\u4e2d\uff0c\u4ee5\u65b9\u4fbf\u4ee3\u7801\u7684\u590d\u7528\u548c\u7ef4\u62a4\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u5bf9PopupWindow\u8fdb\u884c\u66f4\u52a0\u7075\u6d3b\u7684\u5c01\u88c5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PopupWindow\u662fAndroid\u4e2d\u5e38\u7528\u7684\u4e00\u4e2a\u5f39\u51fa\u6846\u63a7\u4ef6\uff0c\u53ef\u4ee5\u7528\u4e8e\u663e\u793a\u4e00\u4e9b\u81ea\u5b9a\u4e49\u7684\u89c6\u56fe\uff0c\u6bd4\u5982\u83dc\u5355\u3001\u63d0\u793a\u6846\u3001\u9009\u62e9\u5668\u7b49\u7b49\u3002PopupWindow\u7684\u4f7f\u7528\u975e\u5e38\u7075\u6d3b\uff0c\u4f46\u662f\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u9700\u8981\u5bf9\u5176\u8fdb\u884c<\/p>\n","protected":false},"author":20,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[430,428,429,156,431],"topic":[],"class_list":{"0":"post-170","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-kaifaapp","7":"tag-ios-","9":"tag-429","10":"tag-156","11":"tag-431"},"_links":{"self":[{"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/posts\/170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/comments?post=170"}],"version-history":[{"count":0,"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/posts\/170\/revisions"}],"wp:attachment":[{"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/media?parent=170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/categories?post=170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/tags?post=170"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/topic?post=170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}