如何在标签内显示地图

时间:2013-10-30 02:58:08

标签: android android-layout android-fragments

我是Android新手,也没有Java经验。 我正在制作一个包含标签的应用程序。其中一个标签在点击时必须显示googlemap。我成功的做到了,问题是当地图显示时,它没有显示标签,所以我无法返回到其他标签。其他选项卡的用途是显示在mapview上选择的位置。谁能帮助我呢?以下是我的代码

MainActivity.java

package jp.co.gpsloader;

    import android.app.Activity;
    import android.app.ActivityGroup;
    import android.content.Context;
    import android.content.Intent;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.widget.TabHost;
    import android.widget.TabHost.TabSpec;
    //Used Activity group because tabhost was used ActivityGroup
    @SuppressWarnings("deprecation")
    public class MainActivity extends ActivityGroup {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // Then do Main Activity name
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_android_tab_layout);
        TabHost tabHost = (TabHost) findViewById(R.id.tabHost);
        tabHost.setup();

        // Then added an Intent on our new tab
        TabSpec spec2 = tabHost.newTabSpec("Location");
        spec2.setIndicator("Location");
        spec2.setContent(new Intent(this, SampleMapClickListener.class));

        TabSpec spec3 = tabHost.newTabSpec("The GPS");
        spec3.setIndicator("The GPS");
        spec3.setContent(new Intent(this, GpsMain.class));


        Context ctx = this.getApplicationContext();
        Intent i = new Intent(ctx, GpsMain.class);
        spec3.setContent(i);

        // Add your new tab in the tabhost here
        tabHost.addTab(spec2);
        tabHost.addTab(spec3);

        // This is needed for Activity group
        tabHost.setup(this.getLocalActivityManager());

        tabHost.setCurrentTab(0);
    }
}

GPSMain.java

package jp.co.gpsloader;

import android.os.*;
import android.widget.Toast;
import android.app.*;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;      
import com.google.android.gms.internal.v;
import com.google.android.gms.maps.*;
import com.google.android.gms.maps.GoogleMap.*;
import com.google.android.gms.maps.model.*;

public class GpsMain extends Activity {
MapFragment mf;
GoogleMap gm;
MarkerOptions marker;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mf = MapFragment.newInstance();
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.add(android.R.id.content, mf);
    ft.commit();
}
public void onResume() {
    super.onResume();

    // created map
    gm = mf.getMap();
    gm.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    gm.setMyLocationEnabled(true);

    // set latitude and longitude
    LatLng ltn = new LatLng(40, 135);

    // create marker
    marker = new MarkerOptions();
    marker.position(ltn);

    // operation can be done, when click happens
    gm.setOnMapClickListener(new SampleMapClickListener());
}

class SampleMapClickListener implements OnMapClickListener {

    @Override
    public void onMapClick(LatLng ltn) {
        marker = new MarkerOptions();
        marker.position(ltn);
        gm = mf.getMap();
        // add marker
        gm.addMarker(marker);

    }

}

}

activity_android_tab_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tabHost"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TabWidget
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs"
/>
 <FrameLayout
 android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabcontent"
 >
 <LinearLayout
 android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tab1"
android:orientation="vertical"
android:paddingTop="60px"
 >
 <TextView  
android:layout_width="fill_parent" 
android:layout_height="100px" 
android:text="This is tab1"
android:id="@+id/txt1"
/>    

 </LinearLayout>

 <LinearLayout
 android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tab2"
android:orientation="vertical"
android:paddingTop="60px"
 >
 <TextView  
android:layout_width="fill_parent" 
android:layout_height="100px" 
android:text="This is tab 2"
android:id="@+id/txt2"
/>

 </LinearLayout>


 </FrameLayout>

</TabHost>

map.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<com.google.android.gms.maps.MapView
    android:id="@+id/mapview"
    android:layout_width="100dip"
    android:layout_height="100dip"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/textView1"
    android:layout_marginRight="15dp" >
</com.google.android.gms.maps.MapView>


</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

在您的activity_android_tab_layout.xml TabWidget上,指定一个高度,比方说50dp:

        android:layout_height="50dp"

在map.xml上,指定marginBottom,也是50dp:

    android:layout_marginBottom="50dp"