如何在Android现有的应用程序添加一个简单的页面
我有一个简单的android应用程序,它是动态创建列表并添加到FragmentManager。列表类是指包含合并布局的布局项目。整个应用程序正在使用SherlockFragmentActivity。如何在Android现有的应用程序添加一个简单的页面
现在我想添加一个永久页脚到现有的FragmentManager而不会影响当前的功能。
这个页脚应该可用的所有页面。
任何人都可以请帮我这么做吗?
这里是在Java代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fm = getSupportFragmentManager();
// Create the list fragment and add it to sole content.
if (fm.findFragmentById(android.R.id.content) == null) {
ClockListFragment list = new ClockListFragment();
fm.beginTransaction().add(android.R.id.content, list).commit();
//adding the list to the content
}
}
这里ClockListFragment是被添加的内容类:
public static class ClockListFragment extends SherlockListFragment implements
LoaderManager.LoaderCallbacks<Cursor>, PauseSource {
variablesdeclarations.......
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setEmptyText(getText(R.string.no_clock_defined));
setHasOptionsMenu(true);
mAdapter = new ClockCursorAdapter(getActivity(), R.layout.world_clock_item, null, this);
setListAdapter(mAdapter);
setListShown(false);
ListView listView = getListView();
setupCabOld(listView);
registerPreferenceChanged();
if (savedInstanceState != null) {
// Restore contextual action bar state
CharSequence cab = savedInstanceState.getCharSequence(CAB);
if (cab != null) {
mMode = getSherlockActivity().startActionMode(new ModeCallback());
mMode.setTitle(cab);
mMode.invalidate();
}
}
getLoaderManager().initLoader(0, null, this);
Clocks.updateOrder(getActivity());
updateWeather(false);
}
........ other codes
}
我有我的活动(S)使用基地布局XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_pane"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/footer_layout">
</FrameLayout>
<include layout="@layout/footer"/>
</RelativeLayout>
然后你可以添加/替换你的片段到@ + id/content_pane(framlayout)。当然你需要一个页脚布局。
要在您的例子中使用:
- 添加的setContentView(R.layout.base);到你的活动onCreate-method。
- 变化
fm.beginTransaction().add(android.R.id.content, list).commit();
到fm.beginTransaction().add(R.id.content_pane, list).commit();
感谢您的回答。我不确定该项目是否具有基础布局。该项目已预定义布局。
添加代码setContentView(R.layout.base_layout)后; \t ClockListFragment list = new ClockListFragment(); \t fm.beginTransaction()。add(R.id.content_pane,list).commit();但我的主要时钟没有显示。我的页脚正在显示。 –
您是否尝试过用'addFooterView()的['ListView'类]的'方法(http://developer.android.com/reference/android/widget/ListView。 HTML)? –
我有两个或两个以上的活动是项目。上面的代码是项目的开始活动。我们在哪里必须使用 –