方式一: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);
}
});
}
动态计算在指定位置添加view,实现引导页效果
于 2018-08-09 15:57:15 首次发布