如何用 LayoutInflater.inflate() 替换 watch view 存根的 findViewById()

How to replace findViewById() with LayoutInflater.inflate() for a watch view stub

我有一个手表应用程序,但正在研究用系统覆盖 window 替换 activity。

充气可穿戴设备布局的标准方法是使用:

 WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);

但是,如果我不再使用 activity,我将在服务器中创建系统覆盖 window。搜索如何执行此所有答案建议使用 LayoutInflator's inflate() 方法。但是,这将资源作为参数,而不是资源 ID。

如何在不使用 findViewById() 的情况下加载 WatchViewStub? 或者有没有办法在服务中使用 findViewById()?

您可以在 Activity 中调用 findViewById(),因为您已经通过 setContentView() 设置了内容视图(根视图)。现在,当您膨胀视图时,您将膨胀作为根的 View,然后在该布局上调用 findViewById(),示例:

View rootView = LayoutInflater.from(context).inflate(R.layout.some_layout, parent, false);
WatchViewStub stub = (WatchViewStub) rootView.findViewById(R.id.watch_view_stub);