开发者

use tabhost by extending MapActivity

开发者 https://www.devze.com 2023-01-16 13:58 出处:网络
main.xml <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\">

main.xml

<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">
    <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"  android:paddingTop="5dip">
            <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" android:text="Loading"/>
            </LinearLayout>
            <RelativeLayout 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="0Ua9BENcUvNKEom7wvjrrtlCVIeJ9SNYAbLAkJA"/>
            </RelativeLayout>
        </FrameLayout>
    </TabWidget>
</TabHost>

public class TabbedListMapActivity extends MapActivity implements OnTabChangeListener {

        private static final String LIST_TAB_TAG = "List";
        private static final String MAP_TAB_TAG = "Map";

         private TabHost tabHost;

        private ListView listView;
        private MapView mapView;
        private  LocalActivityManager mLocalActivityManager ;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
           setContentView(R.layout.main);

            tabHost = (TabHost) findViewById(android.R.id.tabhost);
            tabHost.setup();
            tabHost.setup();
            // LayoutInflater.from(this).inflate(R.layout.maplv, tabHost.getTabContentView(), true);
            // setup must be called if you are not inflating the tabhost from XML

            tabHost.setOnTabChangedListener(this);

            // setup list view
            listView = (ListView) findViewById(R.id.list);
            listView.setEmptyView((TextView) findViewById(R.id.empty));

            // create some dummy coordinates to add to the list
            List<GeoPoint> pointsList = new ArrayList<GeoPoint>();
            pointsList.add(new GeoPoint((int)(32.864*1E6), (int)(-117.2353*1E6)));
            pointsList.add(new GeoPoint((int)(37.441*1E6), (int)(-122.1419*1E6)));
            listView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, pointsList));

            // add an onclicklistener to see point on the map
            listView.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView parent, View view, int position, long id) {
                    GeoPoint geoPoint = (GeoPoint) listView.getAdapter().getItem(position);
                    if(geoPoint != null) {
                        // have map view moved to this point
                        setMapZoomPoint(geoPoint, 12);
                        // programmatically switch tabs to the map view
                        tabHost.setCurrentTab(1);
                    }
                }
            });

            // setup map view
            mapView = (MapView) findViewById(R.id.mapview);
            mapView.setBuiltInZoomControls(true);


            mapView.postInvalidate();

            // add views to tab host
            tabHost.addTab(tabHost.newTabSpec(LIST_TAB_TAG).setIndicator("List").setContent(R.id.list));
            tabHost.addTab(tabHost.newTabSpec(MAP_TAB_TAG).setIndicator("Map").setContent(R.id.mapview));

            //HACK to get the list view to show up first,
            // otherwise the mapview would be bleeding through and visible
            tabHost.setCurrentTab(1);
            tabHost.setCurrentTab(0);
        }

        /**
         * Instructs the map view to navigate to the point and zoom level specified.
         * @param geoPoint
         * @param zoomLevel
         */
        private void setMapZoomPoint(GeoPoint geoPoint, int zoomLevel) {
            mapView.getController().setCenter(geoPoint);
            mapView.getController().setZoom(zoomLevel);
            mapView.postInvalidate();
        }

        /**
         * From MapActivity, we ignore it for this demo
         */
        @Override
        protected boolean isRouteDisplaye开发者_开发技巧d() {
            return false;
        }

        /**
         * Implement logic here when a tab is selected
         */
        public void onTabChanged(String tabName) {
            if(tabName.equals(MAP_TAB_TAG)) {
                //do something on the map
            }
            else if(tabName.equals(LIST_TAB_TAG)) {
                //do something on the list
            }
        } } 

I get a result similar to http://i3.6.cn/cvbnm/e1/71/e9/19008bd7258eaf858be73dc6431b1f8b.jpg. I want to get two tabs; could you help me?


You need to use TabWidget : http://developer.android.com/reference/android/widget/TabWidget.html or http://developer.android.com/guide/tutorials/views/hello-tabwidget.html to show the buttons for the tabs if that's what you are looking for...


i answer my question: tabWidget is wrong and add LinearLayout:

<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="wrap_content" 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" android:text="Loading"/>

        </LinearLayout>
        <RelativeLayout  android:id="@+id/mainlayout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content">
            <com.google.android.maps.MapView android:id="@+id/mapview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" android:apiKey="0Ua9BENcUvNKEom7wvjrrtlCVIeJ9SNYAbLAkJA"/>
        </RelativeLayout>
      </FrameLayout>
  </LinearLayout>

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号