【问题标题】:Get a layout inflater from the ApplicationContext从 ApplicationContext 获取布局充气器
【发布时间】:2011-08-12 16:24:12
【问题描述】:

我正在按照该指南实施自定义首选项。 http://android-journey.blogspot.com/2010/01/for-almost-any-application-we-need-to.html。在方法中

@Override
protected View onCreateView(ViewGroup parent){
   ...
} 

指南以编程方式创建元素,然后返回视图。我想从 xml 文件中检索布局。但是调用getLayoutInflater() 无法访问那里我如何检索布局充气器以获取存储在文件“progressbarpreference.xml”中的视图?

如果需要,我有一个对可用应用程序上下文的静态引用

谢谢

【问题讨论】:

  • 您注册了inflate服务吗?

标签: java android layout


【解决方案1】:
LayoutInflater li = (LayoutInflater) ctx.getSystemService(Service.LAYOUT_INFLATER_SERVICE);

应该可以解决问题(在这种情况下,ctx 是您的应用程序上下文)。

【讨论】:

    【解决方案2】:
    LayoutInflater inflater = LayoutInflater.from(this);
    inflater.inflate(R.layout.yourcustomviewxml, layout);
    

    从我问的一个问题中得到这个:How to pass AttributeSet to custom View 谢谢伊恩!

    【讨论】:

      【解决方案3】:

      使用静态引用,假设它是上下文。

      rowLayout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.progress_bar, parent, false);
      

      其中progress_bar 是线性布局的id。

      【讨论】:

        猜你喜欢
        • 2021-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-26
        相关资源
        最近更新 更多