2

我是android新手,也没有Java经验。我正在制作一个有标签的应用程序。其中一个选项卡在单击时必须显示 googlemap。我成功了,问题是显示地图时,它没有显示选项卡,所以我无法返回其他选项卡。其他选项卡的目的是显示在地图视图上选择的位置。有人可以帮我吗?以下是我的代码

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>

地图.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>
4

1 回答 1

0

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

        android:layout_height="50dp"

在您的 map.xml 上,分配一个 marginBottom,也是 50dp:

    android:layout_marginBottom="50dp"
于 2014-09-03T08:51:16.783 回答