android中在指定的介面中插入引導頁

來源:互聯網
上載者:User

這裡面主要用到的是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();
  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.