我想在選項卡中放置MapActivity和ListActivity。我發現這樣做的一種方式,但我只是不喜歡它的樣子(我寧願用一些類,使我的代碼更加模塊化)ListView和MapActivity下Tab使用類
現在的樣子,我在我的主要活動有這樣的代碼:
Resources res = getResources(); // Resource object to get Drawables
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
intent = new Intent().setClass(this, OptionsActivity.class);
spec = tabHost.newTabSpec(OPTS_TAB_TAG).setIndicator("Options",
res.getDrawable(R.drawable.ic_tab_options))
.setContent(intent);
tabHost.addTab(spec);
而這一次在OptionsActivity類:
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class OptionsActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] options = getResources().getStringArray(R.array.options_array);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, options));
ListView lv = getListView();
lv = (ListView) findViewById(R.id.opt);
lv.setEmptyView((TextView) findViewById(R.id.options));
lv.setTextFilterEnabled(true);
lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, options));
}
}
和我的佈局main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/main">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<TextView
android:id="@+id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"/>
<ListView
android:id="@+id/opt"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<TextView
android:id="@+id/options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"/>
</LinearLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="0fFbO-eNMhj2lDaC6b4YBfpo1DikBMixPYiEJCw" />
</RelativeLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
但由於某種原因,當我選擇這個「選項」選項卡時,我的應用程序有一個關閉...
有人可以告訴我我在做什麼錯嗎?
@lmperian你有兩個列表和地圖在一個活動? –