2

这就是我为实现 CommonsWare 在以下问题中的回答所做的工作: 如何创建一个帮助覆盖,就像您在一些 Android 应用程序和 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 时出错了。感谢帮助,谢谢!

4

1 回答 1

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);
    }
}
于 2014-04-09T12:43:41.987 回答