【问题标题】:Creating a transparent pass-through activity创建透明的传递活动
【发布时间】:2013-01-15 16:39:38
【问题描述】:

我找到了this。所以,远没有运气。我使用了他的代码,但我无法克服这个错误。我不熟悉充气机。那么有人可以解释一下这段代码吗?

final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
            PixelFormat.TRANSLUCENT);

    WindowManager wm = (WindowManager) getApplicationContext()
            .getSystemService(Context.WINDOW_SERVICE);
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewGroup mTopView = (ViewGroup) getApplicationContext()
            .getLayoutInflater().inflate(R.layout.activity_invisible, null);
    getWindow().setAttributes(params);
    wm.addView(mTopView, params);

这部分代码:

ViewGroup mTopView = (ViewGroup) getApplicationContext()
            .getLayoutInflater().inflate(R.layout.activity_invisible, null);

给我这个错误The method getLayoutInflater() is undefined for the type Context

谁能解释我做错了什么?

【问题讨论】:

  • 你在哪里尝试执行这段代码?
  • onCreate() 方法本身。
  • 可能是因为上下文中没有 getLayoutInflater() 方法。例如,尝试使用您的活动。或者,就像你在上面做的那样,getSystemService
  • 我什至不明白,如果你不打算使用那个 layoutinflater,你为什么还要麻烦调用 getSystemService

标签: android layout-inflater


【解决方案1】:

getLayoutInflater()Activity 中的方法,而不是Context

您必须使用 Activity 的实例来调用它,否则它将无法工作。

但是,您可能可以使用在出现错误的行之前创建的 LayoutInflater inflater。比如:

ViewGroup mTopView = (ViewGroup) inflater.inflate(R.layout.activity_invisible, null);

【讨论】:

  • 问题是我在问题中发布的链接使用应用程序上下文。或者,我想。你能检查一下吗?因为他的代码是ViewGroup mTopView = (ViewGroup) App.inflater.inflate(R.layout.main, null); App 到底是什么?
  • App 可能是他的 Application 类的名称。 inflater 很可能是一个静态 LayoutInflater 对象,他在他的 Application 类的 onCreate() 中初始化。
【解决方案2】:

如果您尝试在 onCreate() 中执行此代码,而不是

ViewGroup mTopView = (ViewGroup) getApplicationContext().getLayoutInflater().inflate(R.layout.activity_invisible, null);

试试

ViewGroup mTopView = (ViewGroup) getLayoutInflater().inflate(R.layout.activity_invisible, null);

您的活动具有getLayoutInflater() 方法,因此无需显式获取上下文。

【讨论】:

    猜你喜欢
    • 2013-04-26
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    相关资源
    最近更新 更多