2013-04-06 69 views
2

這是我做了什麼,以實現以下問題CommonsWare的回答是: How do I create a help overlay like you see in a few Android apps and ICS?創建的Android疊加顯示演練

但它失敗。有沒有錯誤,但是當我運行(我做了肯定的isFirstTime()以下功能正常

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    super.onCreate(savedInstanceState); 

if(isFirstTime()) 
{ 
    LayoutInflater inflater = LayoutInflater.from(this); 

    final FrameLayout overlayFrameLayout = new FrameLayout(this); 
    setContentView(overlayFrameLayout); 

    overlayFrameLayout.addView(inflater.inflate(R.layout.main, null)); 
    overlayFrameLayout.addView(inflater.inflate(R.layout.overlay, null)); 

    overlayFrameLayout.setVisibility(View.VISIBLE); 
    overlayFrameLayout.setOnTouchListener(new View.OnTouchListener() 
    { 
     public boolean onTouch(View v, MotionEvent event) 
     { 
      overlayFrameLayout.setVisibility(View.INVISIBLE); 
      overlayFrameLayout.removeViewAt(1); 
      return false; 
     } 
    }); 
} 

setContentView(R.layout.main); 
ctx = getApplicationContext(); 

我敢肯定,我已經錯了創建時沒有出現請注意幫助,謝謝!

回答

0

你沒有看到任何東西,因爲你調用了setContentView兩次,並且R.layout.main被誇大了,並且替換了你以前創建和分配的overlayFrameLayout。下面的代碼應該對它進行排序。

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    super.onCreate(savedInstanceState); 

    if(isFirstTime()){ 
     final LayoutInflater inflater = getLayoutInflater(); 
     final FrameLayout frameLayout = new FrameLayout(this); 
     inflater.inflate(R.layout.main, frameLayout, true); 
     final View overlayView = inflater.inflate(R.layout.overlay, frameLayout, false); 
     overlayView.setOnTouchListener(new View.OnTouchListener() 
     { 
      public boolean onTouch(View v, MotionEvent event) 
      { 
       frameLayout.removeView(overlayView); 
       return false; 
      } 
     }); 
     frameLayout.addView(overlayView); 
     setContentView(frameLayout); 
    } 
    else{ 
     setContentView(R.layout.main); 
    } 
}