動態計算在指定位置新增view,實現引導頁效果
阿新 • • 發佈:2019-01-22
方式一:Popupwindow private void showGuideWindowPop() { if (mGuideWindow == null) { mGuideView = (RelativeLayout) LayoutInflater.from(this) .inflate(R.layout.layout_wallet_guide, null); mGuideWindow = new PopupWindow(mGuideView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); int[] location = new int[2]; sideBarView.promotions.getLocationOnScreen(location);//獲取在整個螢幕內的絕對座標 View view = (View) LayoutInflater.from(this).inflate(R.layout.layout_wallet_guide_view, null); int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); view.measure(w, h); int height = view.getMeasuredHeight(); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params.setMargins(MARGIN_LEFT, location[1] - height, MARGIN_RIGHT, 0); mGuideView.addView(view, params); // 設定點選視窗外邊視窗消失 mGuideWindow.setOutsideTouchable(true); mGuideWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); mGuideWindow.setFocusable(true); mGuideWindow.setClippingEnabled(false); mGuideWindow.showAtLocation(mGuideView, Gravity.CENTER, 0, 0); } Preferences.saveBoolean(Preferences.SHOW_DRAWER_GUIDE, true); mGuideView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mGuideWindow != null && mGuideWindow.isShowing()) { mGuideWindow.dismiss(); } } }); } 方式二:ViewStub <ViewStub android:id="@+id/id_guide_vs" android:inflatedId="@+id/inflatedStart" android:layout_width="match_parent" android:layout_height="match_parent" android:layout="@layout/layout_wallet_guide" /> mViewStub = (ViewStub) findViewById(R.id.id_guide_vs); private void showGuide(){ RelativeLayout rl = (RelativeLayout) mViewStub.inflate(); int[] location = new int[2]; sideBarView.promotions.getLocationOnScreen(location);//獲取在整個螢幕內的絕對座標 View view = (View) LayoutInflater.from(this).inflate(R.layout.layout_wallet_guide_view, null); int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); view.measure(w, h); int height = view.getMeasuredHeight(); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params.setMargins(MARGIN_LEFT, location[1] - height, MARGIN_RIGHT, 0); rl.addView(view, params); Preferences.saveBoolean(Preferences.SHOW_DRAWER_GUIDE, true); rl.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mViewStub.setVisibility(View.GONE); } }); }