{"id":813,"date":"2015-07-01T10:55:12","date_gmt":"2015-07-01T02:55:12","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=813"},"modified":"2021-04-10T16:00:18","modified_gmt":"2021-04-10T08:00:18","slug":"%e8%8e%b7%e5%8f%96%e5%b1%8f%e5%b9%95%e3%80%81%e6%8e%a7%e4%bb%b6%e5%a4%a7%e5%b0%8f%e6%96%b9%e6%b3%95%e6%b1%87%e6%80%bb","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=813","title":{"rendered":"\u83b7\u53d6\u5c4f\u5e55\u3001\u63a7\u4ef6\u5927\u5c0f\u65b9\u6cd5\u6c47\u603b"},"content":{"rendered":"<p>\u5728Android\u4e2d\uff0c\u6709\u65f6\u9700\u8981\u5bf9\u63a7\u4ef6\u8fdb\u884c\u6d4b\u91cf\uff0c\u5f97\u5230\u7684\u63a7\u4ef6\u5bbd\u5ea6\u548c\u9ad8\u5ea6,\u5728OnCreate\u51fd\u6570\u4e2d\u901a\u8fc7\u63a7\u4ef6\u5bf9\u8c61\u8c03\u7528getHeight\u65b9\u6cd5\u83b7\u53d6\u5230\u7684\u4ecd\u7136\u662f0,Measure\u65b9\u6cd5\u4e4b\u540e\u8c03\u7528getMeasuredWidth\u7684\u503c\u8fd8\u662f0\u3002<br \/>\n<!--more--><\/p>\n<p>\u539f\u56e0\u662f\u56e0\u4e3a\u5f53OnCreate\u51fd\u6570\u53d1\u751f\u65f6\uff0c\u53ea\u662f\u63d0\u4f9b\u4e86\u6570\u636e\u521d\u59cb\u5316\u7684\u673a\u4f1a\uff0c\u6b64\u65f6\u8fd8\u6ca1\u6709\u6b63\u5f0f\u7ed8\u5236\u56fe\u5f62\u3002\u800c\u7ed8\u5236\u56fe\u5f62\u5728OnDraw\u4e2d\u8fdb\u884c\uff0c\u6b64\u65f6\u8ba1\u7b97\u53c8\u663e\u5f97\u592a\u665a\u3002\u5bb9\u6613\u60f3\u5230\u7684\u529e\u6cd5\u662f\uff1a\u5e0c\u671b\u80fd\u5728\u7a0b\u5e8f\u521a\u521a\u6d4b\u91cf\u597d\u67d0\u4e2a\u6307\u5b9a\u63a7\u4ef6\u540e\uff0c\u62ff\u5230\u5b83\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\u7acb\u523b\u8fdb\u884c\u8ba1\u7b97\u6216\u6570\u636e\u521d\u59cb\u5316\u3002\u8fd9\u5c31\u9700\u8981\u6709\u4e00\u4e2a\u65b9\u6cd5\u6765\u76d1\u542c\u5230\u8fd9\u4e2a\u4e8b\u4ef6\u7684\u53d1\u751f\uff0c\u5e78\u597dAndroid\u63d0\u4f9b\u4e86\u8fd9\u6837\u7684\u673a\u5236\uff0c\u5229\u7528View\u7c7b\u4e2d\u7684getViewTreeObserver\u65b9\u6cd5\uff0c\u53ef\u4ee5\u83b7\u53d6\u5230\u6307\u5b9aView\u7684\u89c2\u5bdf\u8005\uff0c\u5728\u7ed8\u5236\u63a7\u4ef6\u524d\u7684\u4e00\u5239\u90a3\u8fdb\u884c\u56de\u8c03\uff0c\u8fd9\u6837\u901f\u5ea6\u4e0a\u53c8\u4e0d\u803d\u8bef\uff0c\u5f97\u5230\u7684\u6570\u636e\u7531\u662f\u51c6\u786e\u7684,\u4f46\u6b64\u65b9\u6cd5\u5728\u4e4b\u540e\u53ef\u80fd\u4f1a\u88ab\u53cd\u590d\u8c03\u7528\uff0c\u56e0\u6b64\u9700\u8981\u52a0\u5165\u9650\u5236\uff0c\u666e\u901a\u9700\u6c42\u4e0b\uff0c\u53ea\u8ba1\u7b97\u4e00\u6b21\u5c31\u591f\u4e86\uff0c\u4ee3\u7801\u5982\u4e0b:<br \/>\n<strong>\u4e00\u3001\u83b7\u53d6\u63a7\u4ef6\u5c3a\u5bf8<\/strong><\/p>\n<p>[java]<br \/>\n\u65b9\u6cd5\u4e00:<br \/>\nView layout = findViewById(R.id.popwindow_btn);<br \/>\nViewTreeObserver vto = layout.getViewTreeObserver();<\/p>\n<p>    vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {<br \/>\n\tpublic boolean onPreDraw() {<br \/>\n\t   if (hasMeasured == false) {<\/p>\n<p>\t\tint btWidth = layout.getMeasuredWidth();<br \/>\n\t\tint btHeight = layout.getMeasuredHeight();<br \/>\n\t\thasMeasured = true;<\/p>\n<p>\t  }<br \/>\n\t\treturn true;<br \/>\n\t}<br \/>\n    });<\/p>\n<p>\u65b9\u6cd5\u4e8c:<br \/>\n\tViewTreeObserver vto = mPopupBtn.getViewTreeObserver();<br \/>\n\tvto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {<\/p>\n<p>\t\t@Override<br \/>\n\t\tpublic void onGlobalLayout() {<br \/>\n\t             if (hasMeasured == false) {<br \/>\n\t\t\tint btWidth = mPopupBtn.getMeasuredWidth();<br \/>\n\t\t\tint btHeight = mPopupBtn.getMeasuredHeight();<br \/>\n\t                    hasMeasured = true;<br \/>\n\t\t     }<br \/>\n\t});<\/p>\n<p>\u65b9\u6cd5\u4e09:<br \/>\n\u5c06\u4e00\u4e2arunnable\u6dfb\u52a0\u5230Layout\u961f\u5217\u4e2d\uff1aView.post()<\/p>\n<p>\u7b80\u5355\u5730\u8bf4\uff0c\u53ea\u8981\u7528View.post()\u4e00\u4e2arunnable\u5c31\u53ef\u4ee5\u4e86\u3002runnable\u5bf9\u8c61\u4e2d\u7684\u65b9\u6cd5\u4f1a\u5728View\u7684measure\u3001layout\u7b49\u4e8b\u4ef6\u540e\u89e6\u53d1<\/p>\n<p>UI\u4e8b\u4ef6\u961f\u5217\u4f1a\u6309\u987a\u5e8f\u5904\u7406\u4e8b\u4ef6\u3002\u5728setContentView()\u88ab\u8c03\u7528\u540e\uff0c\u4e8b\u4ef6\u961f\u5217\u4e2d\u4f1a\u5305\u542b\u4e00\u4e2a\u8981\u6c42\u91cd\u65b0layout\u7684message\uff0c\u6240\u4ee5\u4efb\u4f55\u4f60post\u5230\u961f\u5217\u4e2d\u7684\u4e1c\u897f\u90fd\u4f1a\u5728Layout\u53d1\u751f\u53d8\u5316\u540e\u6267\u884c\u3002<\/p>\n<p>[java] final View view=\/\/smth;<br \/>\n &#8230;<br \/>\n view.post(new Runnable() {<br \/>\n             @Override<br \/>\n             public void run() {<br \/>\n                 view.getHeight(); \/\/height is ready<br \/>\n             }<br \/>\n         });<br \/>\n[\/java]<\/p>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u6bd4ViewTreeObserver\u597d\uff1a<br \/>\n1\u3001\u4f60\u7684\u4ee3\u7801\u53ea\u4f1a\u6267\u884c\u4e00\u6b21\uff0c\u800c\u4e14\u4f60\u4e0d\u7528\u5728\u5728\u6bcf\u6b21\u6267\u884c\u540e\u5c06Observer\u7981\u7528\uff0c\u7701\u5fc3\u591a\u4e86\u3002<br \/>\n2\u3001\u8bed\u6cd5\u5f88\u7b80\u5355<\/p>\n<p>\u4e8c\u3001\u83b7\u53d6\u72b6\u6001\u680f\u5c3a\u5bf8<\/p>\n<p>[java]\/**<br \/>\n\t * \u72b6\u6001\u680f\u9ad8\u5ea6<br \/>\n\t *<br \/>\n\t * @return<br \/>\n\t *\/<br \/>\n\tpublic int getStatusBarHeight() {<br \/>\n\t\tRect frame = new Rect();<br \/>\n\t\tgetWindow().getDecorView().getWindowVisibleDisplayFrame(frame);<br \/>\n\t\tint statusBarHeight = frame.top;<\/p>\n<p>\t\treturn statusBarHeight;<br \/>\n\t}[\/java]<\/p>\n<p>\u4e09\u3001\u83b7\u53d6\u6807\u9898\u680f\u5c3a\u5bf8<\/p>\n<p>[java]\t\/**<br \/>\n\t * \u83b7\u53d6\u6807\u9898\u680f\u9ad8\u5ea6<br \/>\n\t *<br \/>\n\t * @return<br \/>\n\t *\/<br \/>\n\tpublic int getTitleBarHeight() {<br \/>\n\t\tint statusBarHeight = getStatusBarHeight();<br \/>\n\t\tint contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT)<br \/>\n\t\t\t\t.getTop();<br \/>\n\t\t\/\/ statusBarHeight\u662f\u4e0a\u9762\u6240\u6c42\u7684\u72b6\u6001\u680f\u7684\u9ad8\u5ea6<br \/>\n\t\tint titleBarHeight = contentTop &#8211; statusBarHeight;<br \/>\n\t\treturn titleBarHeight;<br \/>\n\t}<\/p>\n<p>\tpublic int getTitleBarHeight(){<br \/>\n\t\treturn getActionBar().getHeight();<br \/>\n\t}[\/java]<\/p>\n<p>\u56db\u3001\u83b7\u53d6\u624b\u673a\u5c4f\u5e55\u5c3a\u5bf8<\/p>\n<p>[java]\/**<br \/>\n\t * \u83b7\u53d6\u624b\u673a\u5c4f\u5e55\u5c3a\u5bf8 width,height<br \/>\n\t *<br \/>\n\t * @return<br \/>\n\t *\/<br \/>\n\tpublic int[] getScreenSize() {<br \/>\n\t\tDisplayMetrics dm = new DisplayMetrics();<br \/>\n\t\t\/\/ \u83b7\u53d6\u5c4f\u5e55\u4fe1\u606f<br \/>\n\t\tgetWindowManager().getDefaultDisplay().getMetrics(dm);<br \/>\n\t\tint screenWidth = dm.widthPixels;<br \/>\n\t\tint screenHeigh = dm.heightPixels;<\/p>\n<p>\t\treturn new int[] { screenWidth, screenHeigh };<br \/>\n\t}[\/java]<\/p>\n<p>\u4e94\u3001\u83b7\u53d6\u5185\u5bb9\u533a\u57df\u5c3a\u5bf8<\/p>\n<p>[java]  int contentViewHeight = findViewById(android.R.id.content).getHeight(); \/\/ \u5c4f\u5e55\u5185\u5bb9\u533a\u57df\u9ad8\u5ea6[\/java]<\/p>\n<p>&nbsp;<\/p>\n<p>\u516d\u3001\u5f39\u6846\u5b9e\u4f8b<\/p>\n<p>[java]\/**<br \/>\n\t * \u663e\u793a\u5f39\u51fa\u7a97\u53e3<br \/>\n\t *<br \/>\n\t * @param buttonHeight<br \/>\n\t *\/<br \/>\n\tpublic void showPopupWindow(int buttonHeight) {<br \/>\n\t\tView contentView = getPopWindowContentViewBySimpleAdatper();<\/p>\n<p>\t\t\/\/=============================\u8ba1\u7b97\u5f39\u6846\u5bbd\u9ad8=====================<br \/>\n\t\tint margin = 50;\/\/ \u5f39\u7a97\u8fb9\u8ddd<\/p>\n<p>\t\tint contentViewHeight = findViewById(android.R.id.content).getHeight(); \/\/ \u5c4f\u5e55\u5185\u5bb9\u533a\u57df\u9ad8\u5ea6<\/p>\n<p>\t\tint[] screenSize = getScreenSize();<br \/>\n\t\tint screenWidth = screenSize[0];<br \/>\n\t\tint screenHeigh = screenSize[1];<\/p>\n<p>\t\tint titleHeight = getTitleBarHeight();\/\/ getActionBar().getHeight();\/\/<br \/>\n\t\t\t\t\t\t\t\t\t\t\t\t\/\/ \u83b7\u53d6Title\u9ad8\u5ea6,\u786e\u4fddTitle(ActionBar)\u6ca1\u6709\u9690\u85cf<br \/>\n\t\tint statusBarHeight = getStatusBarHeight();<\/p>\n<p>\t\tint popwindowWidth = screenWidth &#8211; 2 * margin;<br \/>\n\t\tint popwindowHeight = contentViewHeight &#8211; buttonHeight &#8211; margin;<br \/>\n\t\t\/\/ int popwindowHeight = screenHeigh &#8211; buttonHeight &#8211; titleHeight &#8211; statusBarHeight &#8211; margin;<br \/>\n\t\t\/\/====================================================<\/p>\n<p>\t\tmPopwindow = new PopupWindow(contentView, popwindowWidth,popwindowHeight);<\/p>\n<p>\t\t\/\/&#8212;&#8212;&#8212;&#8211;\u5b9e\u73b0\u70b9\u51fb\u7a7a\u767d\u533a\u57df\u5f39\u6846\u6d88\u5931&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n\t\tmPopwindow.setBackgroundDrawable(new BitmapDrawable());<br \/>\n\t\tmPopwindow.setFocusable(true);<br \/>\n\t\tmPopwindow.setOutsideTouchable(true);<br \/>\n\t\t\/\/&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>\t\tmPopupBtn.setOnClickListener(new OnClickListener() {<\/p>\n<p>\t\t\t@Override<br \/>\n\t\t\tpublic void onClick(View v) {<br \/>\n\t\t\t\tif (mPopwindow.isShowing()) {<br \/>\n\t\t\t\t\tmPopwindow.dismiss();<br \/>\n\t\t\t\t} else {<br \/>\n\/\/\t\t\t\t\tmPopwindow.showAsDropDown(v, 10, -5);<\/p>\n<p>\t\t\t\t\tint[] location = new int[2];<br \/>\n\t\t\t        v.getLocationOnScreen(location); \/\/\u83b7\u53d6\u5728\u6574\u4e2a\u5c4f\u5e55\u5185\u7684\u7edd\u5bf9\u5750\u6807  location [0]&#8212;&gt;x\u5750\u6807,location [1]&#8212;&gt;y\u5750\u6807<\/p>\n<p>\t\t\t        Logs.v(&quot;location[0] :&quot;+location[0] + &quot; location[1] :&quot;+location[1]);<\/p>\n<p>\t\t\t        mPopwindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]+50, location[1]+v.getHeight());<br \/>\n\t\t\t\t}<br \/>\n\t\t\t}<br \/>\n\t\t});<\/p>\n<p>\t}[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Android\u4e2d\uff0c\u6709\u65f6\u9700\u8981\u5bf9\u63a7\u4ef6\u8fdb\u884c\u6d4b\u91cf\uff0c\u5f97\u5230\u7684\u63a7\u4ef6\u5bbd\u5ea6\u548c\u9ad8\u5ea6,\u5728OnCreate\u51fd\u6570\u4e2d\u901a\u8fc7\u63a7\u4ef6\u5bf9\u8c61\u8c03\u7528ge [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/813"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=813"}],"version-history":[{"count":9,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/813\/revisions"}],"predecessor-version":[{"id":3222,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/813\/revisions\/3222"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}