【问题标题】:Creating Android overlays for displaying Walk-through创建用于显示演练的 Android 叠加层
【发布时间】:2013-03-29 05:29:15
【问题描述】:

这是我为实现 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 时出错了。感谢您的帮助,谢谢!

【问题讨论】:

    标签: android xml layout user-interface overlay


    【解决方案1】:

    您看不到任何内容,因为您两次调用 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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多