{"id":1492,"date":"2021-09-15T17:35:56","date_gmt":"2021-09-15T09:35:56","guid":{"rendered":"https:\/\/badgameshow.com\/fly\/?p=1492"},"modified":"2021-09-22T15:55:28","modified_gmt":"2021-09-22T07:55:28","slug":"android-popupwindow-%e5%bd%88%e5%87%ba%e5%ae%a2%e8%a3%bd%e5%8c%96%e8%a6%96%e7%aa%97","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/android-popupwindow-%e5%bd%88%e5%87%ba%e5%ae%a2%e8%a3%bd%e5%8c%96%e8%a6%96%e7%aa%97\/","title":{"rendered":"Android PopupWindow \u5f48\u51fa\u5ba2\u88fd\u5316\u8996\u7a97"},"content":{"rendered":"<h1>Android PopupWindow \u5f48\u51fa\u5ba2\u88fd\u5316\u8996\u7a97<\/h1>\n<h5>PopupWindow\u61c9\u7528\u7684\u5834\u666f\u9084\u662f\u883b\u591a\u7684\uff0c\u986f\u793a\u5728activity\u4ecb\u9762\u4e4b\u4e0a\u7684\u4efb\u610f\u5f48\u51fa\u8996\u7a97\u57fa\u672c\u4e0a\u90fd\u53ef\u4ee5\u7528PopupWindow\u4f86\u5be6\u73fe\u3002\u56e0\u70ba\u5b83\u53ef\u4ee5\u986f\u793a\u4efb\u610f\u7684View\u751f\u6210\u4e00\u4e2aPopupWindow\uff0c\u6700\u57fa\u672c\u7684\u4e09\u500b\u689d\u4ef6\u662f\u4e00\u5b9a\u8981\u8a2d\u7f6e\u7684contentView,width,height\u3002<\/h5>\n<hr \/>\n<h4>\u6587\u7ae0\u76ee\u9304<\/h4>\n<ol>\n<li><a href=\"#a\">\u81ea\u5b9a\u7fa9\u8981\u986f\u793a\u7684Layout<\/a><\/li>\n<li><a href=\"#b\">\u5275\u5efaPopupWindow<\/a><\/li>\n<li><a href=\"#c\">PopupWindow\u6dfb\u52a0\u52d5\u756b<\/a><\/li>\n<li><a href=\"#d\">PopupWindow\u986f\u793a\u8996\u7a97\u4f4d\u7f6e<\/a><\/li>\n<li><a href=\"#e\">\u7a0b\u5f0f\u7bc4\u4f8b<\/a><\/li>\n<li><a href=\"#f\">\u6548\u679c\u5c55\u793a<\/a><\/li>\n<\/ol>\n<hr \/>\n<p><a id=\"a\"><\/a><\/p>\n<h4>1.\u81ea\u5b9a\u7fa9\u8981\u986f\u793a\u7684Layout<\/h4>\n<pre><code class=\"language-XML line-numbers\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"wrap_content\"&gt;\n\n    &lt;androidx.constraintlayout.widget.ConstraintLayout\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:layout_marginStart=\"32dp\"\n        android:layout_marginEnd=\"32dp\"\n        android:background=\"#FF00FF\"\n        app:layout_constraintBottom_toBottomOf=\"parent\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toTopOf=\"parent\"&gt;\n\n        &lt;TextView\n            android:id=\"@+id\/a\"\n            android:layout_width=\"0dp\"\n            android:layout_height=\"wrap_content\"\n            android:layout_marginStart=\"32dp\"\n            android:layout_marginEnd=\"32dp\"\n            android:background=\"#FF0000\"\n            android:gravity=\"center\"\n            android:text=\"A\"\n            android:textColor=\"@color\/black\"\n            android:textSize=\"30sp\"\n            app:layout_constraintEnd_toEndOf=\"parent\"\n            app:layout_constraintStart_toStartOf=\"parent\"\n            app:layout_constraintTop_toTopOf=\"parent\" \/&gt;\n\n        &lt;TextView\n            android:id=\"@+id\/b\"\n            android:layout_width=\"0dp\"\n            android:layout_height=\"wrap_content\"\n            android:background=\"#FFFF00\"\n            android:gravity=\"center\"\n            android:text=\"B\"\n            android:textColor=\"@color\/black\"\n            android:textSize=\"30sp\"\n            app:layout_constraintEnd_toEndOf=\"@+id\/a\"\n            app:layout_constraintStart_toStartOf=\"@+id\/a\"\n            app:layout_constraintTop_toBottomOf=\"@+id\/a\" \/&gt;\n\n    &lt;\/androidx.constraintlayout.widget.ConstraintLayout&gt;\n&lt;\/androidx.constraintlayout.widget.ConstraintLayout&gt;\n<\/code><\/pre>\n<p><a id=\"b\"><\/a><\/p>\n<h4>2.\u5275\u5efaPopupWindow(\u5fc5\u8981\u4e09\u500b\u53c3\u6578)<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">val popupWindow = PopupWindow(this).apply {\n    contentView = myContentView\n    width = ViewGroup.LayoutParams.MATCH_PARENT\n    height = ViewGroup.LayoutParams.WRAP_CONTENT\n}\n<\/code><\/pre>\n<p><a id=\"c\"><\/a><\/p>\n<h4>3.PopupWindow\u6dfb\u52a0\u52d5\u756b<\/h4>\n<h4>\u5275\u5efaanim\u8cc7\u6599\u593e<\/h4>\n<p><a href=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2021\/09\/453.png\"><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2021\/09\/453.png\" width=\"70%\"\/><\/a><\/p>\n<h4>\u5275\u5efa\u958b\u555f\u52d5\u756b<\/h4>\n<pre><code class=\"language-XML line-numbers\">&lt;set xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"&gt;\n    &lt;scale\n        android:duration=\"500\"\n        android:fromXScale=\"0.5\"\n        android:fromYScale=\"0.5\"\n        android:pivotX=\"50%\"\n        android:pivotY=\"50%\"\n        android:toXScale=\"1.0\"\n        android:toYScale=\"1.0\" \/&gt;\n    &lt;alpha\n        android:duration=\"500\"\n        android:fromAlpha=\"0.0\"\n        android:toAlpha=\"1.0\" \/&gt;\n&lt;\/set&gt;\n<\/code><\/pre>\n<h4>\u5275\u5efa\u95dc\u9589\u52d5\u756b<\/h4>\n<pre><code class=\"language-XML line-numbers\">&lt;set xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"&gt;\n    &lt;scale\n        android:duration=\"500\"\n        android:fromXScale=\"1.0\"\n        android:fromYScale=\"1.0\"\n        android:pivotX=\"50%\"\n        android:pivotY=\"50%\"\n        android:toXScale=\"0.5\"\n        android:toYScale=\"0.5\" \/&gt;\n    &lt;alpha\n        android:duration=\"500\"\n        android:fromAlpha=\"1.0\"\n        android:toAlpha=\"0.0\" \/&gt;\n&lt;\/set&gt;\n<\/code><\/pre>\n<h4>\u6dfb\u52a0\u5230Style<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">&lt;resources xmlns:tools=\"http:\/\/schemas.android.com\/tools\"&gt;\n    &lt;style name=\"PopupAnimation\" parent=\"android:Animation\"&gt;\n        &lt;item name=\"android:windowEnterAnimation\"&gt;@anim\/popup_enter&lt;\/item&gt;\n        &lt;item name=\"android:windowExitAnimation\"&gt;@anim\/popup_exit&lt;\/item&gt;\n    &lt;\/style&gt;\n&lt;\/resources&gt;\n<\/code><\/pre>\n<p><a id=\"d\"><\/a><\/p>\n<h4>4.PopupWindow\u986f\u793a\u8996\u7a97\u4f4d\u7f6e<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">\/\/\u81ea\u884c\u8a2d\u5b9a\u4f4d\u7f6e\npopupWindow.showAtLocation(view, Gravity.NO_GRAVITY, button.x.toInt(), button.y.toInt() - popupHeight)\n\n\/\/\u9ede\u64caView\u7684\u4e0b\u65b9\npopupWindow.showAsDropDown(view)\n<\/code><\/pre>\n<p><a id=\"e\"><\/a><\/p>\n<h4>5.\u7a0b\u5f0f\u7bc4\u4f8b<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">class MainActivity : AppCompatActivity() {\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        setContentView(R.layout.activity_main)\n\n        val button = findViewById&lt;Button&gt;(R.id.button)\n\n        button.setOnClickListener { view -&gt;\n            val myContentView = layoutInflater.inflate(R.layout.my_pop, null)\n            myContentView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);\n            val popupHeight = myContentView.measuredHeight\n\n            val popupWindow = PopupWindow(this).apply {\n                contentView = myContentView\n                width = ViewGroup.LayoutParams.MATCH_PARENT\n                height = ViewGroup.LayoutParams.WRAP_CONTENT\n                animationStyle = R.style.PopupAnimation\n                \/\/\u6c92\u6dfb\u52a0\u6703\u4e00\u76f4\u5275\u5efa\u65b0\u7684\n                isFocusable = true\n                \/\/\u900f\u660e\u80cc\u666f\n                setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))\n            }\n\n            myContentView.findViewById&lt;TextView&gt;(R.id.a).setOnClickListener {\n                Toast.makeText(this, \"A\", Toast.LENGTH_SHORT).show()\n            }\n            myContentView.findViewById&lt;TextView&gt;(R.id.b).setOnClickListener {\n                Toast.makeText(this, \"B\", Toast.LENGTH_SHORT).show()\n            }\n\n            popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, button.x.toInt(), button.y.toInt() - popupHeight)\n            \/\/popupWindow.showAsDropDown(view)\n        }\n    }\n}\n<\/code><\/pre>\n<p><a id=\"f\"><\/a><\/p>\n<h4>6.\u6548\u679c\u5c55\u793a<\/h4>\n<p><a href=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2021\/09\/video-1631698442.gif\"><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2021\/09\/video-1631698442.gif\" width=\"30%\"\/><\/a><\/p>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Android PopupWindow \u5f48\u51fa\u5ba2\u88fd\u5316\u8996\u7a97 PopupWindow\u61c9\u7528\u7684\u5834\u666f\u9084\u662f\u883b\u591a\u7684\uff0c\u986f\u793a\u5728ac &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[5],"tags":[13,15,201],"class_list":["post-1492","post","type-post","status-publish","format-standard","hentry","category-android","tag-android","tag-kotlin","tag-popupwindow"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1492","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/comments?post=1492"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1492\/revisions"}],"predecessor-version":[{"id":1500,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1492\/revisions\/1500"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=1492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=1492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=1492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}