Android 中的 inflate Activity 和 inflate view 有什么区别?
我似乎不明白是否可以将一项活动膨胀(包含)到另一项活动中。我知道我可以膨胀布局 xml,这有效,但我想知道是否可以膨胀活动。例如,我有一个扩展 Activity 的类 A 和另一个扩展 ListActivity 的类 B。我可以在 A 类、B 类中包含并使用吗? 这就是我尝试过的:
A类:
LayoutInflater inflater = (LayoutInflater) MyActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// inflate list
BActivity list = new BActivity();
B类:
public class BActivity extends ListActivity {
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
List<Model> models= new ArrayList<Model>();
models.add(new Model("John"));
models.add(new Model("Cage"));
setListAdapter(new MyAdapter(this, models));
ListView list = getListView();
}
}
以及在xml中(A类xml):(对于我想查看列表的位置)
<view class="com.test.BActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > </view>
所有这些都会引发错误:
膨胀类 BActivity 时出错
活动在清单中声明。
你知道我做错了什么吗?这不是膨胀另一项活动的正确方法吗?我正在使用 Android 2.2 api 8。 谢谢您的宝贵时间。
I don't seem to understand if it's possible to inflate (include) an activity into another activity. I know i can inflate a layout xml, this works, but i am wondering if i can inflate an activity. For instance , i have class A that extends Activity and another class B that extends ListActivity. Can i include and use in class A, my class B?
THis is what i have tried:
Class A:
LayoutInflater inflater = (LayoutInflater) MyActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// inflate list
BActivity list = new BActivity();
Class B:
public class BActivity extends ListActivity {
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
List<Model> models= new ArrayList<Model>();
models.add(new Model("John"));
models.add(new Model("Cage"));
setListAdapter(new MyAdapter(this, models));
ListView list = getListView();
}
}
and in xml (the class A xml): (for where i want to see the list)
<view class="com.test.BActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > </view>
All of this throws errors :
Error inflating class BActivity
The activities are declared in the manifest.
Do you know what i am doing wrong? this is not the correct way to inflate another activity? I am using Android 2.2 api 8.
Thank you for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的问题标题和你的问题实际上不是一回事。为了完整起见,我将同时回答这两个问题。
答案是没有区别。归根结底,它们在流程和逻辑上是相同的。但是,一个 Activity 可能有许多不同的视图,您可以根据需要多次
setContentView()
到多个不同的布局或视图。活动需要布局资源,视图可能是也可能不是布局。是的。绝对地。
BActivity list = new BActivity();
实际上并未膨胀 Activity。您正在构建活动,但并未启动它。BActivity
定义为View
,但您的代码将其定义为ListActivity
。这是完全不同的两件事。ListActivity
有一个ListView
(扩展或其他);ListActivity
不是ListView
。Activity
及其子类是具有由操作系统管理的生命周期的Context
。它们包含并与所有类型的Views
通信,但它们本身并不是Views。不,先生,但不要害怕!答案并不遥远。
假答案(为了完整性) -
首先,要启动另一个 Activity 使其膨胀,您必须从
Context
调用startActivity()
。Context
可以是应用程序、活动、广播接收器或任何其他应用程序组件(组件 = Android 项目清单中声明的对象)。因此,如果您确实想启动一个新的 Activity,请将BActivity list = new BActivity();
更改为:REAL ANSWER -
但是,由于您想查看 A 类中的 List,因此 BActivity 不是 Activity,而是 View。这意味着您真正想要的是让它识别您的视图,这是一个不同的解决方案。将
public class BActivity extends ListActivity
更改为public class BActivity extends ListView
,现在突然间您就拥有了一个自定义视图!现在我们要做的就是让列表发挥作用。构造视图 - 视图与活动不同,因为它们没有
public void onCreate(Bundle bundle)
。BActivity.onCreate()
中的所有内容都将被放置在构造函数中。但是,你没有合适的构造函数......嗯。好吧,有三个构造函数可供选择 - 添加以下一个或全部(您可能首先需要选项 1 或 2。但是您不会同时使用这两个提示提示,阅读评论:膨胀 Activity = 膨胀视图
您可以在此处进行选择,可以添加视图,也可以膨胀视图。两者都有很多选择。根据你的问题,我假设你想膨胀视图。只需将
BActivity list = new BActivity();
更改为setContentView(R.id.MyXML)
。当然,MyXML 是 XML 布局文件的名称。然后,SetContentView 将使用上面列表中的第二个构造函数为您打开适当的视图 (BActivity)。理解视图和活动之间的区别很重要。它们之间的过程非常相似,但它们本身有一个相互交织但又不同的目的。
LayoutInflater
进行扩充。Activity
有一个名为setContentView
的便捷方法,它可以扩充整个 XML 文件。如需了解更多信息,请阅读 Android 开发者资源。然而,其中一些东西只能通过实验才能学到。
希望这一切都有帮助!
模糊逻辑
Your question title and your issue are not actually the same thing. For completeness, I will answer both.
The answer is there is no difference. Ultimately, they are the same in process and logic. However, an Activity may have many different Views and you may
setContentView()
several times to several different Layouts or Views based on your need. An Activity requires a Layout resource, and a View may or may not be a Layout.Yes. Absolutely.
BActivity list = new BActivity();
is not actually inflating an Activity. You are constructing the Activity, but not starting it.BActivity
as aView
, but your code defines it as anListActivity
. These are two different things entirely. AListActivity
has aListView
(extended or otherwise); AListActivity
is not aListView
.Activity
and its subclasses areContexts
that have a Life Cycle that is managed by the OS. They contain and speak toViews
of all types, but are not themselves Views.No sir, but fear not! The answer is not too far away.
FAKE ANSWER (for completeness) -
First, to start another Activity so that it is inflated, you must call
startActivity()
from aContext
. AContext
may be an Application, Activity, Broadcast Reciever or any other app component (Component = declared object in your Android project manifest). So, if you really wanted to start a new Activity, you would changeBActivity list = new BActivity();
to:REAL ANSWER -
However, since you want to see your List in class A, BActivity is not an Activity, it is a View. That means what you REALLY want is to make it recognize your View and this is a different solution. Change
public class BActivity extends ListActivity
topublic class BActivity extends ListView
and now all of a sudden you have a custom View!! Now all we have to do is get the List to work.Constructing the View - Views are different from Activities in that they do not have a
public void onCreate(Bundle bundle)
. All of your stuff fromBActivity.onCreate()
would instead be placed in the constructor. But, you don't have a proper constructor... hmmm. Well, there are three constructors to choose from -- add one or all of the following (You will probably want either option 1 or 2, at first. But you won't use both at the same time hint hint, read the comments:Inflating the Activity = Inflating the View
You have a choice here, you can either add the View, or you can inflate the View. There are many options for both. Based on your question, I shall assume you want to inflate the View. Simply change
BActivity list = new BActivity();
tosetContentView(R.id.MyXML)
. MyXML, of course, would be the name of your XML Layout file. SetContentView will then open the appropriate View for you (BActivity) using the 2nd constructor from the list above.Understanding the difference between View and Activities is important. The processes between them are very similar, but they themselves have a intertwined but separate purpose.
LayoutInflater
Activity
has a convenience method calledsetContentView
which can inflate an entire XML file.LayoutInflater object.inflate()
.For more information, certainly read more on the Android Developers Resources. However, some of these things are only learned by experimentation.
Hope this all helped!
FuzzicalLogic
膨胀意味着解析对象的 XML 描述并使用所描述的属性构建 Java 对象。活动在 XML 中没有详细的描述,因此没有必要夸大它们。
您可以从活动B中分派活动A,也可以使用Fragment的新概念将多个子活动组合成活动。
顺便说一句,您可以定义自定义视图(通过对视图类之一进行子类化)并在 XML 布局中使用它,只需将类完整路径(例如)放在布局中,而不是说 .
Inflating means parsing an XML description of an object and building a Java object with the described attributes. Activities do not have a detailed description in XML and thus no point to inflate them.
You can dispatch activity A from activity B or you can use the new concept of Fragment to combine multiple sub activities into activities.
BTW, you can define custom views (by subclassing one of the view classes) and use it in your XML layouts, just put the class full path e.g. in the layout instead of let say .