我想建立一个应用程序,将执行操作栏的选项卡。 有一个标签应包含MapFragment。
我怎样才能实现一个操作栏与标签,其中之一是一个地图碎片下?
你能帮助我如何与此继续?
这是我到目前为止有:
大类
package com.nfc.demo;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
public class NFCDemoActivity extends Activity {
Tab selectedTab = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar bar = getActionBar();
bar.setDisplayShowHomeEnabled(false);
bar.setDisplayShowTitleEnabled(false);
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setCustomView(R.layout.main);
ActionBar.Tab mapTab = bar.newTab().setText("Map");
ActionBar.Tab settingsTab = bar.newTab().setText("Settings");
ActionBar.Tab aboutTab = bar.newTab().setText("About");
MapFragment mapFragment = new MapFragment();
SettingsFragment settingsFragment = new SettingsFragment();
AboutFragment aboutFragment = new AboutFragment();
mapTab.setTabListener(new TabListener(mapFragment));
settingsTab.setTabListener(new TabListener(settingsFragment));
aboutTab.setTabListener(new TabListener(aboutFragment));
Tab selectedTab = (Tab) getLastNonConfigurationInstance();
if (selectedTab == null) {
bar.addTab(mapTab, false);
bar.addTab(settingsTab, false);
bar.addTab(aboutTab, true);
}
setContentView(R.layout.main);
}
public Object onRetainNonConfigurationInstance() {
return selectedTab;
}
protected boolean isRouteDisplayed() {
return false;
}
protected class TabListener implements ActionBar.TabListener {
private Fragment fragment;
public TabListener(Fragment fragment) {
this.fragment = fragment;
}
public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
fragmentTransaction.replace(R.id.mainFragment, this.fragment, null);
selectedTab = tab;
}
public void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {
fragmentTransaction.remove(this.fragment);
}
public void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {
//do nothing
}
}
}
片段类都只是返回使用.xml布局吹气。
XML布局:
main.xml中(图应该是在这个XML文件)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
的settings.xml和about.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView123"
android:text="asdfg"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
但加入MapFragment
thows一个错误:
Error inflating class fragment
error caused by java.lang.IllegalArgumentException:
Binary XML file line #2: Duplicate id 0x7f040005, tag null, or parent id 0x1020002 with another fragment for com.google.android.gms.maps.MapFragment 12-28 21:14:07.991: E/AndroidRuntime(26189): at android.app.Activity.onCreateView(Activity.java:4722)
我一直在试图找出如何进行几天,但我真的很困惑。 任何帮助/提示将不胜感激。
另外,怎么样getLastNonConfigurationInstance()
它已被弃用。