android中在指定的界面中插入引导页

这里面主要用到的是WindowManager.LayoutParams的属性及windowManager.addVew(View view,LayoutParams params)的方法

将要显示的引导页的View的Params 设置好,在指定的地方生成guidepage的对像,并调用showGuidePage()方法即可。

public class GuidePage {
 Button mButton;
 Context mContext;
 ImageView mGuideImage;
 WindowManager mWindowManager;
 WindowManager.LayoutParams mImageViewParams;
 WindowManager.LayoutParams mButtonParams;
 /**
  *
  * @param context
  * @param windowManager the windowManager which will get the guidepage.
  * @param resId the drawable resources id for the guidepage to be shown.
  * @param btnText the text that the button of the guidepage will show.
  * @param btnGravity the gravity attribute of the button ,you can use Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL and so on for this param.
  * @return the guidepage instance to show.
  */
 public GuidePage(Context context,WindowManager windowManager,int resId,String btnText,int btnGravity){
  this.mContext = context;
  this.mButton = new Button(mContext);
  this.mButton.setText(btnText);
  this.mGuideImage = new ImageView(mContext);
  this.mGuideImage.setImageResource(resId);
  this.mGuideImage.setScaleType(ImageView.ScaleType.FIT_CENTER);
  this.mWindowManager = windowManager;
  this.mImageViewParams = new LayoutParams();
  this.mImageViewParams.alpha = 0.5f;
  this.mImageViewParams.width = LayoutParams.MATCH_PARENT;
  this.mImageViewParams.height = LayoutParams.MATCH_PARENT;
  this.mImageViewParams.type = 2002;
  this.mButtonParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,2003,40,1);
  this.mButtonParams.gravity = btnGravity;
 }
 /**
  * show the guidepage instance.
  */
 public void showGudiePage(){
  mWindowManager.addView(mButton, mButtonParams);
  mWindowManager.addView(mGuideImage, mImageViewParams);
  mButton.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    mWindowManager.removeView(mButton);
    mWindowManager.removeViewImmediate(mGuideImage);
   }
  });
 }
}

//用类似下面的方法来判断程序或是某个功能是否是第一次启用

void isFirstRun(){
  SharedPreferences sp = mContext.getSharedPreferences("pref_first_run", Context.MODE_PRIVATE);
  boolean b = sp.getBoolean("isFirstRun",true);
  if(b){
   new GuidePage(mContext,mWindowManager,0,null,0).showGudiePage();
   sp.edit().putBoolean("isFirstRun", false).commit();
  }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值