【问题标题】:How to replace findViewById() with LayoutInflater.inflate() for a watch view stub如何将 findViewById() 替换为 LayoutInflater.inflate() 用于查看视图存根
【发布时间】:2015-05-12 22:22:54
【问题描述】:

我有一个手表应用,但正在研究用系统覆盖窗口替换活动。

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

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

但是,如果我不再使用该活动,我将在服务器中创建系统覆盖窗口。搜索如何做到这一点,所有答案都建议使用LayoutInflator's inflate() 方法。但是,它将资源作为参数,而不是资源 ID。

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

【问题讨论】:

    标签: android android-layout android-service wear-os


    【解决方案1】:

    您可以在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);
    

    【讨论】:

      猜你喜欢
      • 2021-03-21
      • 1970-01-01
      • 1970-01-01
      • 2019-06-23
      • 2019-04-02
      • 1970-01-01
      • 2018-10-15
      • 1970-01-01
      • 2015-02-18
      相关资源
      最近更新 更多