我正在嘗試構建一個應用程序來實現Action Bar選項卡。其中一個選項卡應該包含一個MapFragment。MapFragment in Action Bar Tabs
如何使用選項卡實現一個操作欄,其中一個是地圖片段?
你能幫我解決這個問題嗎?
這是我到目前爲止有:
主類
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>
我一直在努力弄清楚如何進行幾天,但我真的很困惑。 任何幫助/提示將不勝感激。
另外,如何getLastNonConfigurationInstance()
?它已被棄用。
對不起,我想我的問題是:明確。我編輯它,但讓我知道是否需要進一步改進。 – hermann
那麼,你的整體問題是很清楚的,但是你給我們提供了一堆代碼,並沒有解釋代碼的問題。將「MapFragment」放在操作欄選項卡中與將任何其他片段放入操作欄選項卡中沒有區別。 – CommonsWare
雖然我似乎無法弄清楚如何去做。一旦我改變了main.xml並添加了一個片段,我得到了由java.lang.IllegalArgumentException引起的錯誤膨脹類片段錯誤:二進制XML文件行#2:重複ID爲0x7f040005,標記爲null,或者父母標識爲0x1020002與另一個fragment for com.google.android.gms.maps.MapFragment 12-28 21:14:07.991:E/AndroidRuntime(26189):\t at android.app.Activity.onCreateView(Activity.java:4722)' – hermann