可以嵌入到您的活动中的UI组件应该从片段而不是活动派生。将子活动转换为片段时,您需要重写onCreateView而不是onCreate以加载片段的布局。
在您的主要活动的布局中,您可以直接插入最初想要显示的片段,并为该片段提供一个ID。然后,您可以使用代码将具有该ID的片段替换为不同的片段。
这是开始的好地方:http://developer.android.com/guide/components/fragments.html
这是太大的话题对我来说,面面俱到 - 你真应该看看Android开发资源 - 但这里有一些例子。
正如我所说的,您可以将初始片段直接放入您的活动布局中。 “片段”位于布局编辑器的“布局”选项卡上。您可以为该片段占位符提供一个“Id”,您可以使用它来标识它以及将在那里开始的片段类的“名称”。在另一个
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment
transaction.replace(R.id.fragment_container, newFragment);
// Commit the transaction
transaction.commit();
你可以使用Fragments :) [Fragments] – 2013-03-20 05:36:17