在我的应用程序中,我有4个选项卡,在其中一个选项卡中我有一个列表(tab3)。当选择其中一个列表选项时,它将转到另一个活动,但随后我删除了选项卡。即使我切换到另一个活动,有没有办法让它们永久存在?
我有想法将标签放在每个XML& Java Class,但这似乎是多余的,我认为它会显示标签在我不想要的不同活动中移动,我希望它们在整个应用程序中保持静止。
代码:
public class MainActivity extends Activity {
TabHost th;
TabSpec specs;
ListView libraryView;
String libraryList[] = {"Item1", "Item2"};
@Override
public void onCreate(Bundle savedInstanceState) {
//Always do this at the start.
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_white_text, R.id.library_list_content, libraryList);
libraryView = (ListView) findViewById(R.id.listView1);
libraryView.setAdapter(adapter);
libraryView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String librarySelected = libraryList[position];
Class libraryClass;
try {
libraryClass = Class.forName("com.jj.test.library." + librarySelected);
Intent libraryIntent = new Intent(MainActivity.this, libraryClass);
startActivity(libraryIntent);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
//Creating the TabHost and gathering its ID for usage.
th = (TabHost) findViewById(R.id.tabhost);
th.setup();
specs = th.newTabSpec("tag1");
specs.setContent(R.id.tab1);
specs.setIndicator("tab1");
th.addTab(specs);
specs = th.newTabSpec("tag2");
specs.setContent(R.id.tab2);
specs.setIndicator("tab2");
th.addTab(specs);
specs = th.newTabSpec("tag3");
specs.setContent(R.id.tab3);
specs.setIndicator("tab3");
th.addTab(specs);
specs = th.newTabSpec("tag4");
specs.setContent(R.id.tab4);
specs.setIndicator("tab4");
th.addTab(specs);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TabHost
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/black" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="@+id/tab4"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
</FrameLayout>
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_marginBottom="-4dp" >
</TabWidget>
</LinearLayout>
</TabHost>
</RelativeLayout>
当他们点击Item1或Item2(在tab3的列表中并将它们带到另一个Activity)时,它不会使用它的标签。我需要对MainActivity XML或Class做些什么才能在整个应用程序中保持这种持久性?