本文介绍了Activity.setContentView,View.setContentView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,活动类具有其中一个xml资源文件可以被加载的setContentView方法。我想做同样的事情,从查看最终继承的类。这似乎是一个死胡同,因为的setContentView方法不适用于View类存在。

I noticed that the Activity class has a setContentView method where an xml resource file can be loaded. I wanted to do the same thing with a class that inherits ultimately from View. This seemed to be a dead end because the setContentView method does not exist for the View class.

这导致了两个问题:

1)是否有可能查看的加载在可视化布局编辑器中创建的布局?

1) Is it possible for View's to load layouts created in the Visual Layout Editor?

2)如果不是,为什么?好像不是允许用户直接加载XML布局到一个视图是一个限制。我期望有为什么在API中未设置的setContentView方法(或类似方法)的一个原因。

2) If not, why? It seems like not allowing users to load an xml layout directly into a View is a limitation. I expect that there is a reason why the setContentView method (or a method similar) is not provided in the API.

在此先感谢!

推荐答案

我觉得LayoutInflator是你在找什么的
这里是一个code样品:

I think that LayoutInflator is what you are looking for http://developer.android.com/reference/android/view/LayoutInflater.htmlAnd here is a code sample:

    View view = (View)inflater.inflate(R.layout.abc, null);
    view_group.add(view);

这篇关于Activity.setContentView,View.setContentView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 21:53