创建用于显示Walk-through的Android叠加层

时间:2013-04-06 22:57:10

标签: android xml layout user-interface overlay

这就是我在以下问题中实现CommonsWare的答案所做的: How do I create a help overlay like you see in a few Android apps and ICS?

但它失败了。没有错误,但是当我跑步时没有出现任何错误(我确保下面的 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();

我很确定在创建FrameLayout时我出错了。感谢您的帮助,谢谢!

1 个答案:

答案 0 :(得分: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);
    }
}